1

At the bottom of the page I have created a custom module in that I want to call getReviewsCollection() function to display information of reviews?

Can anyone please help me?

Bottom of the page custom module is displayed.

I have used https://github.com/MauroNigrele/magento2-test-modules/tree/master/Test/Catalog/view/frontend to create custom module.

Test/Review/registration.php

 <?php
     \Magento\Framework\Component\ComponentRegistrar::register(
     \Magento\Framework\Component\ComponentRegistrar::MODULE,
     'Test_Review',
      __DIR__
 );

Test/Review/etc/module.xml

 <?xml version="1.0"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
 <module name="Test_Review" setup_version="1.0.0">
    <sequence>
        <module name="Magento_Catalog"/>
        <module name="Magento_Review"/>
    </sequence>
 </module>
 </config>

Test/Review/Block/Product/View/Extra.php

  <?php
 namespace Test\Review\Block\Product\View;
 use Magento\Catalog\Block\Product\AbstractProduct;
 class Extra extends AbstractProduct
 {

 }

Test/Review/view/frontend/templates/product/view/extra.phtml

 <?php /* @var $block \Test\Catalog\Block\Product\View\Extra */?>
 <?php $_product = $block->getProduct() ?>
 <h3><?php echo 'My Product Name Is: ' . $_product->getName(); ?></h3>

Test/Review/view/frontend/layout/catalog_product_view.xml

 <?xml version="1.0"?>
<body>
<referenceContainer name="product.info.social">
    <block class="Test\Review\Block\Product\View\Extra"
        name="product.view.extra"
        template="Test_Review::product/view/extra.phtml"
        after="-">
    </block>
</referenceContainer>
</body>
2
  • please show your full code. Jul 21, 2017 at 5:00
  • @Rakesh please see my full code
    – jassi
    Jul 21, 2017 at 5:09

1 Answer 1

1

In your custom module you need to use object of \Magento\Review\Model\ResourceModel\Review\Collection which will give you collection of reviews.

In your Block class Extra , inject \Magento\Review\Model\ResourceModel\Review\Collection in __construct() and use it.

Example:

public function __construct(
...
\Magento\Review\Model\ResourceModel\Review\Collection $reviewCollection
..
) {
..
$this->reviewCollection = $reviewCollection;
..
}
public function getReviewCollection()
{
    return $this->reviewCollection;
}

and call this function getReviewCollection() in your template file using $block->getReviewCollection().

Example:

$reviews = $block->getReviewCollection();
foreach ($reviews as $review) {
    echo $review->getReviewId();
}

Above program is used to return all reviews and not related to a particular product. If you want to get reviews for a particular product you need to follow How we get product rating via product id in magento2?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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