In this article, we are going to guide you on how to keep the item in the wishlist after adding it to the cart. You need to follow two steps, such as create a plugin and a di.xml file in the custom module.
Step -1: Firstly, need to create plugin KeepItemsInWishlistOnPurchase.php in the directory path app/code/Webiators/KeepWishlist/Plugin and add code given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php /** * @category Webiators * @package Webiators_KeepWishlist * @author Webitaors Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://webiators.com ). */ namespace Webiators\KeepWishlist\Plugin; use Magento\Checkout\Model\Cart; use Magento\Wishlist\Model\Item; class KeepItemsInWishlistOnPurchase { /** * @param \Magento\Wishlist\Model\Item $item * @param \Magento\Checkout\Model\Cart $cart * @return array */ public function beforeAddToCart(Item $item, Cart $cart) { return [$cart, false]; } } |
Step – 2: After creating plugin you need to create di.xml in the directory path app/code/Webiators/KeepWishlist/etc/frontend and add the code that given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0"?> <!-- /** * * @category Webiators * @package Webiators_KeepWishlist * @author Webitaors Team * @copyright Copyright (c) Webiators Technologies Private Limited. ( https://webiators.com ). */ --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Wishlist\Model\Item"> <plugin name="webiatorskeepwishlist_in_wishlist" type="Webiators\KeepWishlist\Plugin\KeepItemsInWishlistOnPurchase" sortOrder="1"/> </type> </config> |
We hope that this article helps you to create this functionality for your magento 2 store for providing better customer experience.
Thank You!