4

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.

1
  • How about your issue? Commented Feb 19, 2017 at 11:04

4 Answers 4

4

To 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;
    }
}
1
  • This code runs fine, I'd just change the code from Filename: Vendor/Module/etc/frontend/di.xml to the following so that people don't bump into the 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>
    – Riccardo
    Commented Aug 29, 2021 at 15:15
4

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;
    }
}
2

Go for :Override this method Mage_Wishlist_IndexController::addAction() and replace this:

$this->_redirect('*', array('wishlist_id' => $wishlist->getId())); 

with this:

$this->_redirectReferer()
2
0

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;

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.