0

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>
2

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;
}
| improve this answer | |
2

To get all the reviews for a particular product in the product page, you have to include the following block in your catalog_product_view.xml file.

Step-1)

<block class="Magento\Review\Block\Product\View\ListView" name="product.info.product_additional_data" as="product_review" template="Path to your template file"/>

Step-2)

In your template" file, add the following code,

$_items     =   $block->getReviewsCollection()->getItems();
$format     =   $block->getDateFormat() ?: \IntlDateFormatter::SHORT;
$review     =   array();
if (count($_items)) {

    foreach ($_items as $_review){

        if (count($_review->getRatingVotes())) {
            foreach ($_review->getRatingVotes() as $_vote) {
                $rating_vote =  $_vote->getPercent();
                $rating_code =  $block->escapeHtml($_vote->getRatingCode());
            }
        } else {
            $rating_vote = 0;
            $rating_code = '';
        }

        $review[] =     array(
                            'title'             =>  $block->escapeHtml($_review->getTitle()), 
                            'review-text'       =>  $block->escapeHtml($_review->getDetail()),
                            'review-by-title'   =>  __('Review by'),
                            'rating-code'       =>  $rating_code,
                            'review-by'         =>  $block->escapeHtml($_review->getNickname()),
                            'rating-value'      =>  $rating_vote,
                            'review-date'       =>  $block->formatDate($_review->getCreatedAt(), $format)
                        );

    }

}

$content['item']            =   $review;
$content['review-count']    =   count($_items);
$content['review-title']    =   __('Customer Reviews');

echo $content ;

Step-3)

In your Product Page phtml, access the reviews as follows,

$review_array                   =   $this->getChildHtml('product_review');

Print the $review_array and you can see all reviews.

| improve this answer | |
  • In step 1, you have mentioned the core template file as list .phtml, Now In step 2 you told to add the code what you mentioned in list.phtml. But list.phtml is a core file how could i add your code in that – Jaisa Dec 12 '17 at 6:22
  • You told me to do override – Jaisa Dec 12 '17 at 6:22
  • See my post, I have already added a custom container for showing the reviews. If I am wrong Please correct me – Jaisa Dec 12 '17 at 6:24
  • You don't have to use or override Magento_Review::product/view/list.phtml. You can use whatever phtml file you want, but you have to bind the block Magento\Review\Block\Product\View\ListView to your phtml. Then in your phtml file , use the function $block->getReviewsCollection()->getItems(); to get all the reviews. – MGento Dec 12 '17 at 6:27
0

In your layout file, you can add a block using the different unique identifier.

You can add the review block again using a different name

<block class="Magento\Review\Block\Product\Review" name="reviews.list" as="reviews.list" template="Magento_Review::review.phtml" />
| improve this answer | |
  • Thank you for your response. I have created the section to display the customer reviews ,Please see the updated post – Jaisa Dec 12 '17 at 4:55
  • In my code i need to call customer reviews collection in abc.phtml from the block – Jaisa Dec 12 '17 at 4:56
  • Add above block in your prefered container with updating the path template="XXX_YYY::abc.phtml" – Salma Saiyad Dec 12 '17 at 4:58
  • I have added the block ,But how to get the reviews collection in block – Jaisa Dec 12 '17 at 4:59
  • <container name="custom.product.section"> <block class="Magento\Review\Block\Product\Review" name="custom.product.section.info" template="XXX_YYY::abc.phtml"/> </container> – Salma Saiyad Dec 12 '17 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.