Magento – How to get the all the reviews of the product posted by the customer in magento 2

collection;frontendmagento2.2PHPproduct-review

I have removed review tab and placed my review form as a popup modal in product page.How to get the all the reviews of the product posted by the customer in Product page.Now I need to get all the reviews collection and want to displayed it in product page. Please provide me a solution

catalog_product_view.xml

<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>

        <referenceBlock name="product.info.review">
            <action method="setTemplate">
                <argument name="template" xsi:type="string">XXX_YYY::review.phtml</argument>
            </action>

            <block class="Magento\Framework\View\Element\Template" name="custom.review.popup" template="XXX_YYY::popup.phtml">

                <block class="Magento\Review\Block\Form" name="custom.product.review.form" as="custom.review_form" template="XXX_YYY::form.phtml">
                    <container name="product.review.form.fields.before" as="form_fields_before" label="Review Form Fields Before"/>
                </block>
            </block>

        </referenceBlock>
        <referenceBlock name="product.info.details">
            <referenceBlock name="reviews.tab" remove="true" />
        </referenceBlock>
        <!--Created new section before the footer-->
        <referenceContainer name="page.bottom.container">
            <container name="custom.product.section">
                <block class="XXX_YYY\Block\Form" name="custom.product.section.info" template="XXX_YYY::abc.phtml"/>
            </container>
        </referenceContainer>
    </body>
</page>

Best Answer

Look this block file : /vendor/magento/module-review/Block/Product/View.php

In this file, Magento create the function getReviewsCollection()

public function getReviewsCollection()
{
    if (null === $this->_reviewsCollection) {
        $this->_reviewsCollection = $this->_reviewsColFactory->create()->addStoreFilter(
            $this->_storeManager->getStore()->getId()
        )->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $this->getProduct()->getId()
        )->setDateOrder();
    }
    return $this->_reviewsCollection;
}
Related Topic