1

I am currently building a custom customer account management module. In the dashboard, I want to display the recent product reviews made by the logged in customer. I just can't find my way in getting those reviews made by specific customer.

I am getting reviews by product this way:

public function getProductReviewDetails($product){
    $reviewDetails = array();
    $rf = $this->reviewFactory->create()->getEntitySummary($product, $this->store->getId());
    $reviews = $this->review->getResourceCollection()->addStoreFilter($this->store->getId())->addEntityFilter('product', $product->getId())->setDateOrder()->addRateVotes()->getItems();
    $rating_summary = $product->getRatingSummary()->getRatingSummary();
    $reviewDetails = array(
        'count' => count($reviews),
        'list' => $reviews,
        'ratingSummary' => ($rating_summary == null || !$rating_summary) ? 100 : $rating_summary
    );
    return $reviewDetails;
}

So I tried editing that into :

public function getProductReviews(){
        $reviewDetails = array();
        $customerId = $this->customerSession->getCustomer()->getId();
        $reviews = $this->review->getResourceCollection()->addStoreFilter($this->store->getId())->addEntityFilter('customer', $customerId)->setDateOrder()->addRateVotes()->getItems();
        $reviewDetails = array(
            'count' => count($reviews),
            'list' => $reviews
        );
        return $reviewDetails;
    }

But still I can't get those reviews.

  • What's the count yielding? – Julien Lachal Nov 10 '16 at 10:22
  • 1
    I'm getting array(2) { ["count"]=> int(0) ["list"]=> array(0) { } } – reuelab Nov 10 '16 at 10:25
3

Finally, I got it to work.

I looked into Magento\Review\Model\ResourceModel\Review\Collection and found this method

/**
     * Add customer filter
     *
     * @param int|string $customerId
     * @return $this
     */
    public function addCustomerFilter($customerId)
    {
        $this->addFilter('customer', $this->getConnection()->quoteInto('detail.customer_id=?', $customerId), 'string');
        return $this;
    }
| improve this answer | |
0

We can get customer id by product review like this!

    protected $_reviewCollection;

public function __construct(
    \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewCollection
){
    $this->_reviewCollection = $reviewCollection;
}

public function getReviewByCustomer($customerId)
{
    $collection = $this->_reviewCollection->create()
    ->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
    ->addCustomerFilter($customerId)
    ->setDateOrder()->addRateVotes();
    $collection->getData();

    foreach ($collection as $review) {
        print_r($review->getData());exit();
    }
}
| improve this answer | |

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.