1

Here I don't want to use a object manager.
Below code give me result as i aspect.but I don't want to use a object manager.
I don't known how to convert into factory method.can any one me out.
Here is my code.

<?php
 $id= 12; // product id
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $product = $objectManager->create("Magento\Catalog\Model\Product")->load($id);
 $rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
 $collection = $rating->create()
    ->addStatusFilter(
        \Magento\Review\Model\Review::STATUS_APPROVED
    )->addEntityFilter(
        'product',
         $id
    )->setDateOrder();
   echo "<pre>";
 print_r($collection->getData());
 ?>
1

Here is your converted code

namespace Test\Module\Block;
use Magento\Catalog\Api\ProductRepositoryInterface;
class Product extends \Magento\Framework\View\Element\Template
 {
      protected $productRepository; 
      protected $_reviewCollection; 

      public function __construct(
        \Magento\Framework\App\Action\Context $context,
        ProductRepositoryInterface $productRepository,
       \Magento\Review\Model\ResourceModel\Review\CollectionFactory   $reviewCollection
      ) {
          parent::__construct($context);

          $this->productRepository = $productRepository;
          $this->_reviewCollection = $reviewCollection;
      }
      public function getProduct()
      {

          $productId=1;
           $product = $this->productRepository->getById($productId);
           $collection = $this->_reviewCollection->create()
              ->addStatusFilter(
                      \Magento\Review\Model\Review::STATUS_APPROVED
               )->addEntityFilter(
            'product',
              $productId
             )->setDateOrder();
             echo "<pre>";
             print_r($collection->getData());
      }
  }

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.