I need to add to wishlist ajax based or customer should not redirect to wishlist page with out redirect in Magento 2 environment.
anyone have suggestion please share.
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityTo fix it we need to override Magento\Wishlist\Controller\Index\Add controller.
– add following line in module di.xml to tell magento to use our class instead of standard one.
Filename: Vendor/Module/etc/frontend/di.xml
<preference for="Magento\Wishlist\Controller\Index\Add" type="Vendor\Module\Controller\Index\Add">
</preference>
– create the class. Basically it duplicate original method, only $resultRedirect updated to use referer url
Filename: Vendor/Module/Controller/Index/Add.php
<?php
namespace Vendor\Module\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
{
/**
* Adding new item
*
* @return \Magento\Framework\Controller\Result\Redirect
* @throws NotFoundException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function execute()
{
...
/* here comes the code from original controller */
...
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}
Warning: DOMXPath::query(): Invalid expression
error. <?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="Vendor\Module\Controller\Index\Add"/> </config>
Another solution would be to add Plugin. Add di.xml
in etc directory
<type name="Magento\Wishlist\Controller\Index\Add">
<plugin name="custom_wishlist_redirect" type="Vendor\ModuleName\Plugin\Wishlist\AddPlugin" sortOrder="10" />
</type>
Add Plugin Class
namespace Vendor\ModuleName\Plugin\Wishlist;
use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Wishlist\Controller\Index\Add;
class AddPlugin
{
/**
* @var RedirectInterface
*/
private $redirectIntrface;
/**
* AddPlugin constructor.
* @param RedirectInterface $redirectIntrface
*/
public function __construct(
RedirectInterface $redirectIntrface
) {
$this->redirectIntrface = $redirectIntrface;
}
/**
* @param Add $subject
* @param Redirect $resultRedirect
* @return Redirect
*/
public function afterExecute(
Add $subject,
Redirect $resultRedirect
): Redirect
{
$resultRedirect->setUrl($this->redirectIntrface->getRefererUrl());
return $resultRedirect;
}
}
Go for :Override this method Mage_Wishlist_IndexController::addAction()
and replace this:
$this->_redirect('*', array('wishlist_id' => $wishlist->getId()));
with this:
$this->_redirectReferer()
This is may not be a good way, however, you can override Magento\Wishlist\Controller\Index\Add
. The custom execute method should return the Referer Url.
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;