Magento – Getting Product Reviews by Customer Id

customermagento2review

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.

Best Answer

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;
    }
Related Topic