1

I do not want the customers/guest to let post reviews who did not make a purchase of that product.

  • for guest users I disable - "Allow guest to write reviews"

  • for customers/logged in users - how to add a condition to check customer has purchased a particular product at least once?

Those who have purchased a particular product that should be able to post review from a product page.

2

As you have wanted to show review form only login customer who has purchased that product.

Then you have to stop render of the block Magento\Review\Block\Form on sales order item collection.

First, you have to create a plugin on toHtml() at Magento\Review\Block\Form and use aroundToHtml() method to stop html render.

Plugin Class

<?php

namespace {Vendor}\{Modulename}\Plugin\Review\Block;


class FormPlugin {

    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;

    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory
     */
    protected $itemCollectionFactory;

    public function __construct(
            \Magento\Sales\Model\ResourceModel\Order\Item\CollectionFactory $itemCollectionFactory,
            \Magento\Framework\App\Http\Context $httpContext
            ) {

        $this->itemCollectionFactory = $itemCollectionFactory;
        $this->httpContext = $httpContext;
    }
    public function aroundToHtml(
        \Magento\Review\Block\Form $subject,
        \Closure $proceed       
    )
    {
        $product = $subject->getProductInfo();
        $itemCollection = $this->itemCollectionFactory->create();
        $itemCollection->addFieldToSelect('product_id')->addFieldToFilter('product_id', $product->getId());
        if($itemCollection->count() >0 ){
//Make string to object.
          return $proceed();  

        }

        return '';
    }
}

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.