Question

I want to display pending Review in the product review tab. But I can not found the proper file to remove or edit filter for pending review. Thank you in advance.

Was it helpful?

Solution

The review list which displayed on product view tab was defined here

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Review/Block/Product/View.php#L122-L135

public function getReviewsCollection()
{
    if (null === $this->_reviewsCollection) {
        $this->_reviewsCollection = $this->_reviewsColFactory->create()->addStoreFilter(
            $this->_storeManager->getStore()->getId()
        )->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $this->getProduct()->getId()
        )->setDateOrder();
    }
    return $this->_reviewsCollection;
}

You could try to override the block and add filter to the

    ->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )

as what ever status you want

If you want to filter both approved and pending review

See

https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Review/Model/ResourceModel/Review/Collection.php#L209-L225

 /**
 * Add status filter
 *
 * @param int|string $status
 * @return $this
 */
public function addStatusFilter($status)
{
    if (is_string($status)) {
        $statuses = array_flip($this->_reviewData->getReviewStatuses());
        $status = isset($statuses[$status]) ? $statuses[$status] : 0;
    }
    if (is_numeric($status)) {
        $this->addFilter('status', $this->getConnection()->quoteInto('main_table.status_id=?', $status), 'string');
    }
    return $this;
}

As a new overridden Collection we could write another filter like - This will define filter approved and pending to current collection. This was just and idea, you could use this filter directly to the overridden Block:

 /**
 * Add approved and pending status filter
 *
 * @return $this
 */
public function addApprovedAndPendingStatusFilter()
{
    $statuses = [\Magento\Review\Model\Review::STATUS_APPROVED, \Magento\Review\Model\Review::STATUS_PENDING];
    $inCond = $this->getConnection()->prepareSqlCondition('main_table.status_id', ['in' => $statuses]);

    $this->getSelect()->where($inCond);
    return $this;
}

Then back to the overridden Block we could call:

public function getReviewsCollection()
{
    if (null === $this->_reviewsCollection) {
        $this->_reviewsCollection = $this->_reviewsColFactory->create()->addStoreFilter(
            $this->_storeManager->getStore()->getId()
        )->addApprovedAndPendingStatusFilter()->addEntityFilter(
            'product',
            $this->getProduct()->getId()
        )->setDateOrder();
    }
    return $this->_reviewsCollection;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top