0

I tried to override add wishlist controller from the custom module but it's not working.

app/code/Demo/Wishlist/etc/frontend/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"> 
<preference for="Magento\Wishlist\Controller\Index\Add" type="Demo\Wishlist\Controller\Index\Add"/>
</config>

app/code/Demo/Wishlist/Controller/Index/Add.php

 <?php
namespace Demo\Wishlist\Controller\Index;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;
/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magento\Wishlist\Controller\Index\Add
{
 /**
 * @var \Magento\Wishlist\Controller\WishlistProviderInterface
 */
 protected $wishlistProvider;
 /**
 * @var \Magento\Customer\Model\Session
 */
 protected $_customerSession;
 /**
 * @var ProductRepositoryInterface
 */
 protected $productRepository;
 /**
 * @var Validator
 */
 protected $formKeyValidator;
 /**
 * @param Action\Context $context
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
 * @param ProductRepositoryInterface $productRepository
 * @param Validator $formKeyValidator
 */
 public function __construct(
 Action\Context $context,
 \Magento\Customer\Model\Session $customerSession,
 \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
 ProductRepositoryInterface $productRepository,
 Validator $formKeyValidator
 ) {
 $this->_customerSession = $customerSession;
 $this->wishlistProvider = $wishlistProvider;
 $this->productRepository = $productRepository;
 $this->formKeyValidator = $formKeyValidator;
 parent::__construct($context, $customerSession, $wishlistProvider, $productRepository, $formKeyValidator);
 }
 /**
 * Adding new item
 *
 * @return \Magento\Framework\Controller\Result\Redirect
 * @throws NotFoundException
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
 */
 public function execute()
 {
 echo "From custom module calling";
 exit;
 }
}

I have followed the below reference link in the clean default magento2.3.2 EE but no luck.

http://hkpatel201.blogspot.com/2017/02/magento-2-overriderewrite-wishlist-controller.html

Can any one know the reason what I was missed here?

asked Jan 30, 2020 at 9:55

3 Answers 3

1

EE Version should using MultipleWishlist instead of wishlist, so you should override MultipleWishlist

<preference for="Magento\MultipleWishlist\Controller\Index\Add"
 type="Custom\Module\Controller\Wishlist\Add"/>
answered Jul 8, 2020 at 7:31
0

You have to pass dependency to parent class in construct of child controller.

Please use below code

 public function __construct(
 Action\Context $context,
 \Magento\Customer\Model\Session $customerSession,
 \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
 ProductRepositoryInterface $productRepository,
 Validator $formKeyValidator
 ) {
 $this->_customerSession = $customerSession; 
 $this->wishlistProvider = $wishlistProvider;
 $this->productRepository = $productRepository;
 $this->formKeyValidator = $formKeyValidator;
 parent::__construct($context, $customerSession, $wishlistProvider, $productRepository, $formKeyValidator);
 }
answered Jan 30, 2020 at 10:04
9
  • Thanks @prasant let me check Commented Jan 30, 2020 at 10:10
  • No @prasant it's not working Commented Jan 30, 2020 at 10:57
  • i have tired this aswell but no luck hkpatel201.blogspot.com/2017/02/… can you help me Commented Jan 30, 2020 at 11:33
  • @NagarajuK, What error you're getting if you're using this solution ? Commented Jan 30, 2020 at 12:45
  • No error @KishanSavaliya still vendor module changes only calling. Commented Jan 30, 2020 at 12:47
0

Try to use below code in your di.xml file

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Magento\Wishlist\Controller\Index\Add" type="Vendor\Module\Controller\Wishlist\Add"/>
</config>

app/code/Vendor/Module/Controller/Wishlist/Add.php

Content for this file is..

<?php
namespace Vendor\Module\Controller\Wishlist;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Controller\ResultFactory;
/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magento\Wishlist\Controller\Index\Add
{
 /**
 * @var \Magento\Wishlist\Controller\WishlistProviderInterface
 */
 protected $wishlistProvider;
 /**
 * @var \Magento\Customer\Model\Session
 */
 protected $_customerSession;
 /**
 * @var ProductRepositoryInterface
 */
 protected $productRepository;
 /**
 * @var Validator
 */
 protected $formKeyValidator;
 /**
 * @param Action\Context $context
 * @param \Magento\Customer\Model\Session $customerSession
 * @param \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider
 * @param ProductRepositoryInterface $productRepository
 * @param Validator $formKeyValidator
 */
 public function __construct(
 Action\Context $context,
 \Magento\Customer\Model\Session $customerSession,
 \Magento\Wishlist\Controller\WishlistProviderInterface $wishlistProvider,
 ProductRepositoryInterface $productRepository,
 Validator $formKeyValidator
 ) {
 $this->_customerSession = $customerSession;
 $this->wishlistProvider = $wishlistProvider;
 $this->productRepository = $productRepository;
 $this->formKeyValidator = $formKeyValidator;
 parent::__construct($context, $customerSession, $wishlistProvider, $productRepository, $formKeyValidator);
 }
 /**
 * Adding new item
 *
 * @return \Magento\Framework\Controller\Result\Redirect
 * @throws NotFoundException
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 * @SuppressWarnings(PHPMD.UnusedLocalVariable)
 */
 public function execute()
 {
 echo "From custom module wishlist has been calling";
 exit; 
 }
}

Hope this will help you!

answered Jan 30, 2020 at 12:55
13
  • 1
    I will go through this & let u know in the 10 mints Commented Jan 30, 2020 at 13:24
  • No luck @krishan its not working Commented Jan 30, 2020 at 14:34
  • scope of the di.xml whether we need to change? Commented Jan 30, 2020 at 14:36
  • Can you please check where you're adding di.xml and above php file that module is enabled or not ? Commented Jan 30, 2020 at 14:54
  • app/code/Demo/WishlistRestriction/etc/di.xml I have tried Commented Jan 30, 2020 at 17:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.