Question

How can i add every product as a new item into cart instead of updating cart quantity?

ie. Suppose a product is already added in cart. If i add that product again to cart it should add new product not only updating quantity of that product.

Was it helpful?

Solution

I got the solution.

we need to override the Quote.php file(Magento\Quote\Model\Quote.php) and should manage if condition in addProduct() method.

 //if (!$item) {
            $item = $this->itemProcessor->init($candidate, $request);
            // Add only item that is not in quote already
            $this->addItem($item);
 //}

Then each product will be add as new item into cart instead of updating existing item

OTHER TIPS

There is a method in \Magento\Quote\Model\Quote\Item class called representProduct. This method compare if item was added before or not. So you need to create plugin which should always return false for adding each item as new.

  1. Add a plugin in etc/di.xml file of your module
<type name="Magento\Quote\Model\Quote\Item">
    <plugin name="add_each_item_separately"
            type="Vendor\Module\Plugin\Quote\Model\Quote\ItemPlugin"
            sortOrder="10"/>
</type>
  1. Create plugin class in Vendor\Module\Plugin\Quote\Model
namespace Vendor\Module\Plugin\Quote\Model\Quote;

use Magento\Quote\Model\Quote\Item;

class ItemPlugin
{
    public function afterRepresentProduct(Item $item, bool $result): bool
    {
        return false;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top