3

I need to keep the wishlist items after the customer have added them to the cart.

Where is the actual removal being done in the code?
I'm trying to track the call, but gets lost in the post being made in Wishlist.php in getAddAllToCartParams() method.

7

In app/code/Magento/Wishlist/Controller/Index/Cart.php in the execute() method the following code is what adds the item to the cart and removes it from the wishlist:

$item->addToCart($this->cart, true);

The addToCart() method is declared in app/code/Magento/Wishlist/Model/Item.php and the second parameter is what triggers the wishlist deletion. If you set it to false it won't delete the item from the wishlist:

/**
 * Add or Move item product to shopping cart
 *
 * Return true if product was successful added or exception with code
 * Return false for disabled or unvisible products
 *
 * @param \Magento\Checkout\Model\Cart $cart
 * @param bool $delete  delete the item after successful add to cart
 * @return bool
 * @throws \Magento\Catalog\Model\Product\Exception
 */
public function addToCart(\Magento\Checkout\Model\Cart $cart, $delete = false)
{
    ...
    if ($delete) {
        $this->delete();
    }
}

To avoid the auto delete behavior I suggest you create a plugin on the app/code/Magento/Wishlist/Model/Item.php class:

vendor\Module\etc\di.xml

<?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="disableAutoDelete" type="Vendor\Module\Plugin\DisableAutoDelete" />
    </type>
</config>

Then in your Plugin/DisableAutoDelete.php file you can do:

<?php
    namespace Vendor\Module\Plugin;
    
    class DisableAutoDelete
    {
        public function beforeAddToCart(\Magento\Wishlist\Model\Item $subject, \Magento\Checkout\Model\Cart $cart, $delete) {
            return [$cart, false];
        }
    }
?>
5
  • @maxo feel free to mark your question as answered so it'll benefit for others Jul 5 '16 at 13:31
  • Thank you for the terrific answer complete with the plugin suggestions. It worked like a charm with one edit: <?php and ?> around the code in DisableAutoDelete.php (not obvious for a php noob like me).
    – maxo
    Jul 5 '16 at 14:09
  • @maxo pretty sure you don't even need the last ?> glad you get it working, feel free to upvote my answer =) Jul 5 '16 at 14:11
  • Okay, good to know.
    – maxo
    Jul 5 '16 at 14:12
  • tried by editing params in app/code/Magento/Wishlist/Controller/Index/Cart.php and app/code/Magento/Wishlist/Model/Item.php classes by setting value to false didn't work for me But plugin helps but need some changes in di.xml and also answer is updated according to that (1+ vote) for answer
    – Asad Ullah
    Jun 29 '20 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.