Magento – Magento 2 – Don’t want to remove wishlist items when click ‘Add all to cart’ button

addtocartmagento2wishlist

I want to keep items in wishlist when user click "add all to cart" button. Have any idea how to fix it?

Best Answer

Magento adds wishlist items to the cart using the addToCart() method on the wishlist Item class. This method has a parameter that specifies whether to also remove the item from the wishlist. You can add a plugin to this to force this parameter to always be false.

Add this to a custom module di.xml, replacing Namespace\Module with your module name:

<?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\Model\Item">
        <plugin name="keep_in_wishlist" type="Namespace\Module\Plugin\KeepItemsInWishlistOnPurchase" />
    </type>
</config>

Then add this plugin to your module:

<?php
namespace Namespace\Module\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];
    }
}
Related Topic