{"id":1431,"date":"2020-11-30T09:56:43","date_gmt":"2020-11-30T09:56:43","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-to-wishlist-without-redirecting-to-wishlist-page-in-magento-2\/"},"modified":"2025-07-17T10:20:00","modified_gmt":"2025-07-17T04:50:00","slug":"add-to-wishlist-without-redirecting-to-wishlist-page-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-to-wishlist-without-redirecting-to-wishlist-page-in-magento-2\/","title":{"rendered":"How to &#8220;Add to Wishlist&#8221; Without Redirecting to Wish List Page in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 store owners focus on providing a fabulous shopping experience to customers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With fierce competition in E-commerce, the store owners have become more cautious about delivering a smooth and convenient shopping experience as it helps to retain customers and attract new customers too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Even a small improvement on-site can drastically affect sales. For example, redirecting users to another page on user action.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the default\u00a0Magento 2, while adding a product to a wishlist using the \u201cAdd to Wishlist\u201d button, he\/she is redirected to the wishlist page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, it creates a hurdle in letting a visitor browse through more products. This not only degrades the shopping experience but also distracts the potential customer from adding more products to the wishlist and eventually cart. Hence, it adversely affects the sales of Magento 2 store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To avoid such unnecessary redirection, I have come up with a programmatic to&nbsp;<em><strong>\u201cAdd to Wishlist\u201d without redirection to wish list page in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution for \u201cAdd to Wishlist\u201d without redirection to wish list page in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create di.xml file at MeetanshiExtensionetc folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:ObjectManager\/etc\/config.xsd\">\n    &lt;preference for=\"Magento\\Wishlist\\Controller\\Index\\Add\" type=\"Meetanshi\\Extension\\Controller\\Index\\Add\">\n    &lt;\/preference>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create file Add.php file at MeetanshiExtensionControllerIndex<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Controller\\Index;\n\nuse Magento\\Catalog\\Api\\ProductRepositoryInterface;\nuse Magento\\Framework\\App\\Action;\nuse Magento\\Framework\\Data\\Form\\FormKey\\Validator;\nuse Magento\\Framework\\Exception\\NotFoundException;\nuse Magento\\Framework\\Exception\\NoSuchEntityException;\nuse Magento\\Framework\\Controller\\ResultFactory;\n\nclass Add extends \\Magento\\Wishlist\\Controller\\Index\\Add\n{\n    public function execute()\n    {\n        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);\n        if (!$this->formKeyValidator->validate($this->getRequest())) {\n            return $resultRedirect->setPath('*\/');\n        }\n\n        $wishlist = $this->wishlistProvider->getWishlist();\n        if (!$wishlist) {\n            throw new NotFoundException(__('Page not found.'));\n        }\n\n        $session = $this->_customerSession;\n\n        $requestParams = $this->getRequest()->getParams();\n\n        if ($session->getBeforeWishlistRequest()) {\n            $requestParams = $session->getBeforeWishlistRequest();\n            $session->unsBeforeWishlistRequest();\n        }\n\n        $productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;\n        if (!$productId) {\n            $resultRedirect->setPath('*\/');\n            return $resultRedirect;\n        }\n\n        try {\n            $product = $this->productRepository->getById($productId);\n        } catch (NoSuchEntityException $e) {\n            $product = null;\n        }\n\n        if (!$product || !$product->isVisibleInCatalog()) {\n            $this->messageManager->addErrorMessage(__('We can\\'t specify a product.'));\n            $resultRedirect->setPath('*\/');\n            return $resultRedirect;\n        }\n\n        try {\n            $buyRequest = new \\Magento\\Framework\\DataObject($requestParams);\n\n            $result = $wishlist->addNewItem($product, $buyRequest);\n            if (is_string($result)) {\n                throw new \\Magento\\Framework\\Exception\\LocalizedException(__($result));\n            }\n            if ($wishlist->isObjectNew()) {\n                $wishlist->save();\n            }\n            $this->_eventManager->dispatch(\n                'wishlist_add_product',\n                ['wishlist' => $wishlist, 'product' => $product, 'item' => $result]\n            );\n\n            $referer = $session->getBeforeWishlistUrl();\n            if ($referer) {\n                $session->setBeforeWishlistUrl(null);\n            } else {\n                $referer = $this->_redirect->getRefererUrl();\n            }\n\n            $this->_objectManager->get(\\Magento\\Wishlist\\Helper\\Data::class)->calculate();\n\n            $this->messageManager->addComplexSuccessMessage(\n                'addProductSuccessMessage',\n                [\n                    'product_name' => $product->getName(),\n                    'referer' => $referer\n                ]\n            );\n        } catch (\\Magento\\Framework\\Exception\\LocalizedException $e) {\n            $this->messageManager->addErrorMessage(\n                __('We can\\'t add the item to Wish List right now: %1.', $e->getMessage())\n            );\n        } catch (\\Exception $e) {\n            $this->messageManager->addExceptionMessage(\n                $e,\n                __('We can\\'t add the item to Wish List right now.')\n            );\n        }\n\n        $resultRedirect->setUrl($this->_redirect->getRefererUrl());\n        return $resultRedirect;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s what you need to do for keeping the customers on the same page they are surfing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I hope you it helps.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Do not forget to share this post with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 store owners focus on providing a fabulous shopping experience to customers. With fierce competition in E-commerce, the store owners have become more cautious&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1431","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1431"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1431\/revisions"}],"predecessor-version":[{"id":13921,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1431\/revisions\/13921"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1431"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1431"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}