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".
}
}