Magento2 根据产品id获取评论信息

Magento2 根据产品id获取评论信息

产品的评论信息这里是在控制器里面展示操作的:本案例是在控制器里面操作,如下代码所以:

<?php
namespace  ChuWu\Christmas\Controller\Index;

use Magento\Framework\App\ResponseInterface;

class Review extends \Magento\Framework\App\Action\Action
{

    public function execute()
    {
        // TODO: Implement execute() method.

        //初始化对象管理器
        $objectManager  = \Magento\Framework\App\ObjectManager::getInstance();

        //获取店铺id
        $storeManager   = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
        $currentStoreId = $storeManager->getStore()->getId();

        //获取产品对象
         $productId = 326;
        //$productId = 1674;
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);

      //echo   $product->getTypeId();

       // echo "商品id是:".$product->getId();
        echo "商品名称是:".$product->getName();
        echo "<br>商品sku是:".$product->getSku();
        //获取评论模型
        $reviewFactory = $objectManager->create('Magento\Review\Model\Review');

        //传递产品对象和店铺id给评论模型)
        $reviewFactory->getEntitySummary($product, $currentStoreId);

        //获取的评分(展示审核通过确认显示的评论,评分是多个评分的平均值)
        $ratingSummary = $product->getRatingSummary()->getRatingSummary();
        echo "<br>平均评分:".$ratingSummary;
        //获取评论数量(评论通过总的评论数量)
        $reviewCount = $product->getRatingSummary()->getReviewsCount();
        echo  "<br>获取评论数量".$reviewCount.'<br>';

        //获取每个评论的具体信息
        $reviewsCollection = $reviewFactory->getCollection()->addFieldToSelect('*')
            ->addStoreFilter($currentStoreId)
            ->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
            ->addEntityFilter('product', $productId)
            ->setDateOrder();

        $reviews =$reviewsCollection->load()->addRateVotes();
        foreach ($reviews as $review) {
            $rateVotes = $review->getRatingVotes()->getItems();
            foreach ($rateVotes as $votes) {
                $vote = $votes->getValue();
                $review->setData('rating', $vote);
            }
        }
        echo "<pre>";
        print_r($reviews->getData());
        echo "</pre>";
        foreach ($reviews  as $key => $reviewItem){

            echo "评论信息如下:";
            echo "<br>Rating:".$reviewItem->getRating();
            echo "<br>Nickname:".$reviewItem->getNickname();
            echo "<br>title:".$reviewItem->getTitle();
            echo "<br>CreatedAt:".$reviewItem->getCreatedAt();
            echo "<br>Detail:".$reviewItem->getDetail();
            echo "<br>";
        }
        die('');
    }
}

输出结果如下:

商品名称是:Corner Small Makeup Vanity Table Flip Mirror
商品sku是:HG61A0455/HG61U0451
平均评分:100
获取评论数量2
Array
(
    [0] => Array
        (
            [review_id] => 23
            [created_at] => 2020-03-18 07:48:46
            [entity_id] => 1
            [entity_pk_value] => 326
            [status_id] => 1
            [detail_id] => 23
            [title] => Small but sturdy, Love it.
            [detail] => Get this for my 16 years old girls and she love it so much. Even the dressing table is small, it is sturdy enough to put her things.
            [nickname] => Lindsay
            [customer_id] => 
            [entity_code] => product
        )

    [1] => Array
        (
            [review_id] => 22
            [created_at] => 2020-03-18 07:36:47
            [entity_id] => 1
            [entity_pk_value] => 326
            [status_id] => 1
            [detail_id] => 22
            [title] => Versatile, work as a working table & a dressing table
            [detail] => After serious consideration, I finally choose this one. The reason why I choose is that my bedroom is small, and this Small Makeup Vanity Table is so befitting. I could make my lipstick, liquid foundation, eyebrow pencil etc to place in the box. The mirror is reversible, and I can make it as a working table to study.  Look Great!
            [nickname] => Judy
            [customer_id] => 
            [entity_code] => product
        )

)
评论信息如下:
Rating:5
Nickname:Lindsay
title:Small but sturdy, Love it.
CreatedAt:2020-03-18 07:48:46
Detail:Get this for my 16 years old girls and she love it so much. Even the dressing table is small, it is sturdy enough to put her things.
评论信息如下:
Rating:5
Nickname:Judy
title:Versatile, work as a working table & a dressing table
CreatedAt:2020-03-18 07:36:47
Detail:After serious consideration, I finally choose this one. The reason why I choose is that my bedroom is small, and this Small Makeup Vanity Table is so befitting. I could make my lipstick, liquid foundation, eyebrow pencil etc to place in the box. The mirror is reversible, and I can make it as a working table to study. Look Great!

输出结果如下截图: