Magento 2 Add to wish list return to the product detail page

Magento 2 Add to wish list return to the product detail page

August 10, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 Add to wish list return to the product detail page

Overriding Magento 2 Wishlist Controller: A Comprehensive Guide

In Magento 2, when you add a product to your wishlist, it automatically redirects you to the wishlist page instead of staying on the product detail page. This default behavior can be frustrating for some users who want to continue browsing without leaving their current product view.

To achieve this custom functionality, you’ll need to override the Magento\Wishlist\Controller\Index\Add controller and make a few adjustments. Let’s walk through the process step by step.

Understanding the Current Implementation

Before diving into the solution, let’s first understand what the current code looks like:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Eguana_CustomWishlist" setup_version="2.1.3">
        <sequence>
            <module name="Magento_Wishlist" />
        </sequence>
    </module>
</config>

The Eguana_CustomWishlist module is set to load after the Magento_Wishlist module in the dependency sequence. However, despite this configuration, Magento 2 is still calling the default controller.

The Issues with the Current Approach

There are a couple of issues with the current implementation that might be causing the problem:

  1. Di Preference Positioning: The di preference for overriding the Add controller might not be properly positioned within the module’s configuration files, leading Magento 2 to ignore it.

  2. Controller Method Execution: In your overridden execute() method, you’re simply echoing a string without actually handling the redirection logic. This could be causing the default behavior to remain intact.

The Correct Approach

To successfully override the wishlist controller and redirect back to the product detail page, you’ll need to make the following changes:

Step 1: Create the Override Controller File

Create a new file named Add.php in your custom module’s controller directory (Eguana/CustomWishlist/Controller/Index). This file should extend the default Magento\Wishlist\Controller\Index\Add class.

namespace Eguana\CustomWishlist\Controller\Rewrite\Index;

use Magento\Framework\App\Action;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Framework\Exception\NotFoundException;
use Magento\Framework\Controller\ResultFactory;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class Add extends \Magento\Wishlist\Controller\Index\Add
{
    // ... Other class properties ...

    /**
     * Adding new item
     *
     * @return \Magento\Framework\Controller\ResultInterface
     * @throws NotFoundException
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    public function execute()
    {
        try {
            // ... Existing code ...

            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl($product->getProductUrl());
            return $resultRedirect;
        } catch (NoSuchEntityException $e) {
            throw new NotFoundException(__('Product does not exist'));
        }
    }

    // ... Other methods ...
}

Step 2: Update the di.xml File

Update your di.xml file to correctly position the preference for the Add controller.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/di.xsd">
    <type name="Magento\Wishlist\Controller\Index\Add">
        <arguments>
            <argument name="wishlistProvider" xsi:type="object">Eguana\CustomWishlist\Controller\Rewrite\Index\Add</argument>
        </arguments>
    </type>
</config>

Step 3: Clear Cache and Compile

After making the necessary changes, ensure you clear your Magento cache and run the bin/magento setup:di:compile command to apply the new overrides.

Conclusion

Overriding the wishlist controller in Magento 2 requires a deep understanding of the dependency injection system and proper positioning of the override files. By following this comprehensive guide, you can successfully redirect users back to the product detail page after adding an item to their wishlist.

Remember, always make sure your module is properly loaded after the Magento_Wishlist module in the dependency sequence, and carefully handle redirection logic within your overridden methods.

For more advanced customizations and best practices, I recommend exploring Laravel Company’s blog on Magento development.

Happy coding!

Magento 2 Catalog Extensions Configuration

Our network