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?
3 Answers 3
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"/>
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);
}
-
Thanks @prasant let me checkNagaraju Kasa– Nagaraju Kasa2020年01月30日 10:10:09 +00:00Commented Jan 30, 2020 at 10:10
-
No @prasant it's not workingNagaraju Kasa– Nagaraju Kasa2020年01月30日 10:57:49 +00:00Commented Jan 30, 2020 at 10:57
-
i have tired this aswell but no luck hkpatel201.blogspot.com/2017/02/… can you help meNagaraju Kasa– Nagaraju Kasa2020年01月30日 11:33:00 +00:00Commented Jan 30, 2020 at 11:33
-
@NagarajuK, What error you're getting if you're using this solution ?Kishan Savaliya– Kishan Savaliya2020年01月30日 12:45:33 +00:00Commented Jan 30, 2020 at 12:45
-
No error @KishanSavaliya still vendor module changes only calling.Nagaraju Kasa– Nagaraju Kasa2020年01月30日 12:47:28 +00:00Commented Jan 30, 2020 at 12:47
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!
-
1I will go through this & let u know in the 10 mintsNagaraju Kasa– Nagaraju Kasa2020年01月30日 13:24:31 +00:00Commented Jan 30, 2020 at 13:24
-
No luck @krishan its not workingNagaraju Kasa– Nagaraju Kasa2020年01月30日 14:34:14 +00:00Commented Jan 30, 2020 at 14:34
-
scope of the di.xml whether we need to change?Nagaraju Kasa– Nagaraju Kasa2020年01月30日 14:36:58 +00:00Commented 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 ?Kishan Savaliya– Kishan Savaliya2020年01月30日 14:54:31 +00:00Commented Jan 30, 2020 at 14:54
-
app/code/Demo/WishlistRestriction/etc/di.xml I have triedNagaraju Kasa– Nagaraju Kasa2020年01月30日 17:26:57 +00:00Commented Jan 30, 2020 at 17:26
Explore related questions
See similar questions with these tags.