Blog

Do not redirect “Add to Wishlist” in Magento 2

Today we are going to learn about How to “Add to Wishlist” Without Redirecting to Wishlist Page in Magento 2.

Add to Wishlist provides customers the option to add their products to the wishlist, makes their future purchases, send this list to the friends, more easily.

Current Behaviour:

In the default Magento 2, while adding a product to a wishlist using the “Add to Wishlist” button, customer gets redirected to the wishlist page.

Dis-advantages of current behaviour:

This interrupts the shopping journey of customers and the customer may not even come back to surf other products. To avoid this, you can eliminate wishlist page redirection when the customer clicks on “Add to Wishlist”.

So let’s look at the steps on How to “Add to Wishlist” Without Redirecting to Wishlist Page in Magento 2.

Plugins makes it easy to change or add the required code in the file. So lets see how we can achieve this with the help of plugins.

Steps to “Do not redirect “Add to Wishlist” in Magento 2”:

Let’s try this step by step. Follow the steps to get the exact result.

Step 1: Create the custom module and the module.xml and registration.php file.

Create File  app/code/Thecoachsmb/Wishlist/etc/module.xml

with content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Thecoachsmb_Wishlist" > 
     <sequence> 
         <module name="Magento_Wishlist"/> 
     </sequence>
 </module> 
</config>

To register the module, create a registration.php file in the app/code/Thecoachsmb/Wishlist/registration.php

Contents would be:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
	\Magento\Framework\Component\ComponentRegistrar::MODULE,
	'Thecoachsmb_Wishlist',
	__DIR__
);

Step 2: First you need to go to the below path app\code\Thecoachsmb\Wishlist\etc\di.xml

And add the below code

<?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="thecoachsmb_wishlist_redirect" type="\Thecoachsmb\Wishlist\Plugin\Controller\Index\Add"/>
     </type>
</config>

 

Step 3: Next, navigate to the below path  app\code\Thecoachsmb\Wishlist\Plugin\Controller\Index\Add.php

And add the code as mentioned below:

<?php

namespace Thecoachsmb\Wishlist\Plugin\Controller\Index;

use Magento\Framework\App\Response\RedirectInterface;
use Magento\Framework\Controller\Result\Redirect;

class Add
{
    /**
     * @var RedirectInterface
     */
     private $redirectIntrface;

     /**
      * Add constructor.
      * @param RedirectInterface $redirectIntrface
      */
      public function __construct(
          RedirectInterface $redirectIntrface
       ) {
               $this->redirectIntrface = $redirectIntrface;
         }

      /**
        * @param Add $subject
        * @param Redirect $resultRedirect
        * @return Redirect
        */
        public function afterExecute(\Magento\Wishlist\Controller\Index\Add $subject, Redirect $resultRedirect): Redirect
        {
             $resultRedirect->setUrl($this->redirectIntrface->getRefererUrl());
             return $resultRedirect;
        }
}

Run the below command:

php bin/magento module:enable Thecoachsmb_Wishlist && php bin/magento se:up && php bin/magento se:s:d -f

With these three steps, it becomes easy for customers for adding product to the wishlist and also stay on the same page where he can perform different operations.

Hope this was helpful, see you in the next article.

Leave a Reply

Your email address will not be published. Required fields are marked *