1

I would like to stop adding products to the wishlist based on the product attribute how to do that? I would like to use the plugin concept instead of overriding the entire controller file. Magento\Wishlist\Controller\Index\Add. But I unable to stop redirecting to a wishlist page after satisfied my condition. Please review and let me know how to do that?

app/code/Vendor/Wishlist/etc/di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
       <type name="Magento\Wishlist\Controller\Index\Add">
           <plugin name="AddWishlistRestriction" type="Vendor\Wishlist\Plugin\AddWishlist" sortOrder="10" disabled="false"/>
       </type>
    </config>

app/code/Vendor/Wishlist/Plugin/AddWishlist.php

    public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        dataHelper $dataHelper,
        \Magento\Framework\App\Action\Context $context,
        ProductRepositoryInterface $productRepository

    ) {  
        $this->dataHelper         = $dataHelper;
        $this->messageManager     = $messageManager;
        $this->redirect           = $redirect;
        $this->productRepository  = $productRepository;
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->request = $context->getRequest();


    }

    /**
     * Plugin for restrict wishlist 
    */
    public function beforeExecute(\Magento\Wishlist\Controller\Index\Add $subject)
    {
        echo "before execute method is calling".
    }
}

1 Answer 1

0

I have found the solution by the following below code using aroundExecute() method since it has returns value instead of beforeExecute() and afterExecute() methods.

Now It's working as expected.

<?php    
namespace Vendor\Wishlist\Plugin;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Request\Http;
use Vendor\Wishlist\Helper\Data as dataHelper;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Message\ManagerInterface;
/**
 * Class AddWishlist
 * @package Vendor\Wishlist\Plugin
 */
class AddWishlist
{
    /**
     * @var dataHelper
    */
    protected $dataHelper;

    /**
     * @var ManagerInterface
    */
    protected $messageManager;

    protected $redirect;

    /**
     * @var ProductRepositoryInterface
    */
    protected $productRepository;

    public function __construct(
        \Magento\Framework\Message\ManagerInterface $messageManager,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        dataHelper $dataHelper,
        \Magento\Framework\App\Action\Context $context,
        ProductRepositoryInterface $productRepository
    ) {  
        $this->dataHelper         = $dataHelper;
        $this->messageManager     = $messageManager;
        $this->redirect           = $redirect;
        $this->productRepository  = $productRepository;
        $this->resultRedirectFactory = $context->getResultRedirectFactory();
        $this->request = $context->getRequest();
    }

    /**
     * Plugin for restrict wishlist 
    */
    public function aroundExecute(\Magento\Wishlist\Controller\Index\Add $subject, \Closure $proceed)
    {              
        $requestParams = $this->request->getParams();
        $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;
        if (!$productId) {
            $resultRedirect->setPath('*/');
            return $resultRedirect;
        }
        try {
            $product = $this->productRepository->getById($productId);
        } catch (NoSuchEntityException $e) {
            $product = null;
        }

        /*If wishlist restriction enabled */
        if($this->dataHelper->getWishlistRestrictionModuleEnabled() && $product)
        {
            $wishlistStoreConfigValues=[];
            $productAttributeValues =[];
            $data = explode(',',$product->getData('availability'));
            foreach($data as $value) {
                $attr=$product->getResource()->getAttribute('availability');
                if ($attr->usesSource()) {
                    $productAttributeValues[] = $attr->getSource()->getOptionText($value);
                }
            }
            $wishlistStoreConfigValues=$this->dataHelper->getWishlistRestrictionValues();
            $restrictionFlag = (count(array_intersect($wishlistStoreConfigValues, $productAttributeValues))) ? 'restricted' : 'notrestricted';
                if($restrictionFlag === 'restricted') {
                    $resultRedirect = $this->resultRedirectFactory->create();
                    $this->messageManager->addNotice(__('Sorry, You can\'t add this product to wishlist'));
                    $resultRedirect = $this->resultRedirectFactory->create();
                    $resultRedirect->setUrl($this->redirect->getRefererUrl());
                    return $resultRedirect; 
                }
        }
        return $proceed();
    }
}

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.