How to “Add to Wishlist” Without Redirecting to Wish List Page in Magento 2
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 about delivering a smooth and convenient shopping experience as it helps to retain customers and attract new customers too.
Even a small improvement on-site can drastically affect sales. For example, redirecting users to another page on user action.
In the default Magento 2, while adding a product to a wishlist using the “Add to Wishlist” button, he/she is redirected to the wishlist page.
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.
To avoid such unnecessary redirection, I have come up with a programmatic to “Add to Wishlist” without redirection to wish list page in Magento 2.
Solution for “Add to Wishlist” without redirection to wish list page in Magento 2:
- Create di.xml file at Meetanshi\Extension\etc folder
123456<?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="Meetanshi\Extension\Controller\Index\Add"></preference></config> - Create file Add.php file at Meetanshi\Extension\Controller\Index
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<?phpnamespace Meetanshi\Extension\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;class Add extends \Magento\Wishlist\Controller\Index\Add{public function execute(){$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);if (!$this->formKeyValidator->validate($this->getRequest())) {return $resultRedirect->setPath('*/');}$wishlist = $this->wishlistProvider->getWishlist();if (!$wishlist) {throw new NotFoundException(__('Page not found.'));}$session = $this->_customerSession;$requestParams = $this->getRequest()->getParams();if ($session->getBeforeWishlistRequest()) {$requestParams = $session->getBeforeWishlistRequest();$session->unsBeforeWishlistRequest();}$productId = isset($requestParams['product']) ? (int)$requestParams['product'] : null;if (!$productId) {$resultRedirect->setPath('*/');return $resultRedirect;}try {$product = $this->productRepository->getById($productId);} catch (NoSuchEntityException $e) {$product = null;}if (!$product || !$product->isVisibleInCatalog()) {$this->messageManager->addErrorMessage(__('We can\'t specify a product.'));$resultRedirect->setPath('*/');return $resultRedirect;}try {$buyRequest = new \Magento\Framework\DataObject($requestParams);$result = $wishlist->addNewItem($product, $buyRequest);if (is_string($result)) {throw new \Magento\Framework\Exception\LocalizedException(__($result));}if ($wishlist->isObjectNew()) {$wishlist->save();}$this->_eventManager->dispatch('wishlist_add_product',['wishlist' => $wishlist, 'product' => $product, 'item' => $result]);$referer = $session->getBeforeWishlistUrl();if ($referer) {$session->setBeforeWishlistUrl(null);} else {$referer = $this->_redirect->getRefererUrl();}$this->_objectManager->get(\Magento\Wishlist\Helper\Data::class)->calculate();$this->messageManager->addComplexSuccessMessage('addProductSuccessMessage',['product_name' => $product->getName(),'referer' => $referer]);} catch (\Magento\Framework\Exception\LocalizedException $e) {$this->messageManager->addErrorMessage(__('We can\'t add the item to Wish List right now: %1.', $e->getMessage()));} catch (\Exception $e) {$this->messageManager->addExceptionMessage($e,__('We can\'t add the item to Wish List right now.'));}$resultRedirect->setUrl($this->_redirect->getRefererUrl());return $resultRedirect;}}
That’s what you need to do for keeping the customers on the same page they are surfing.
I hope you it helps.
If you have questions regarding this post, feel free to ask in the Comment section below.
I would be happy to answer your questions.
Do not forget to share this post with Magento Community via social media.
Thank you.
Related Post:
Jignesh Parmar
An expert in his field, Jignesh is the team leader at Meetanshi and a certified Magento developer. His passion for Magento has inspired others in the team too. Apart from work, he is a cricket lover.
Prev
How to Add Custom Customer Attribute in Registration Form in Magento 2
Meetanshi Magento Extensions Launches and Updates November [2020]
Next