0

how can we stop the execution using plugin based on the condition. I am using aroundExecute plugin method.

<?php
namespace Custom\Reviews\Plugin\Controller\Adminhtml\Product;
use Magento\Framework\App\RequestInterface;
use Magento\Review\Model\ReviewFactory;
use Webkul\Walletsystem\Model\WalletUpdateData;
class Save
{



    protected $request;

    protected $resource;

    protected $reviewFactory;

    /**
     * @var Webkul\Walletsystem\Model\WalletUpdateData
     */
    private $walletUpdate;
    private $walletHelper;
    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
    */
    protected $date;
            
    public function __construct(
        RequestInterface $request,
        \Magento\Framework\App\ResourceConnection $resource,
        ReviewFactory $reviewFactory,
        WalletUpdateData $walletUpdate,
        \Webkul\Walletsystem\Helper\Data $walletHelper,
        \Magento\Framework\Stdlib\DateTime\DateTime $date
    ){
        $this->request = $request;
        $this->_resource = $resource;
        $this->reviewFactory = $reviewFactory;
        $this->walletUpdate = $walletUpdate;
        $this->walletHelper = $walletHelper;
        $this->date = $date;
    }
    public function aroundExecute(
        \Magento\Review\Controller\Adminhtml\Product\Save $subject,
        callable $proceed
    ){
        $reviewId = $this->request->getParam('id');
        $review = $this->reviewFactory->create()->load($reviewId);
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/satish.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $timestamp = date("Y-m-d h:i:s");
        if ($review['status_id'] == 1) { // if review already Approved then no need to change the status
            // code...
            $logger->info("Already approved want to show msg like this status not be changed.");
        }else{

            $logger->info("functionality added while approving");

        }

        $result = $proceed();

        return $result;
    }
}

2 Answers 2

1

To Restrict the review status execution you can simply return blank instead of $proceed() or throw exception. Refer below code. Didn't tested but i have done same thing many times.

if ($review['status_id'] == 1) { // if review already Approved then no need to change the status
        // code...
               // for custom message you could throw an exception here like as below.
      throw new StateException(__('Already Review approved.'))

        
    } else {

        return $proceed();

    }

For custom message, i recommend to use Magento Exception which just not stop execution but it will show message as an error. probably try "StateException" may help.

1
   if ($review['status_id'] == 1 ) {
                $this->messageManager->addNoticeMessage(__('Already approved.'));
                $resultRedirect= $this->resultRedirectFactory->create();
                $resultRedirect->setPath('*/*/');
                return $resultRedirect;
    }else{
        $result = $proceed();
        return $result;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.