Magento 2 – Prevent Increment When Adding Multiple Products with Same SKU to Cart

b2bcartmagento2magento2.2.3multiple-checkout

But instead build new line items. For instance if it's the same product just list it twice on the cart page.

enter image description here

As you can see in the picture: the "Portia Capri-29-Blue" have qty 2. But instead of it to increment in the qty box just list each one as a seperate line item.

And for the reason why. Here's what I'm trying to accomplish. using the b2b features in m2 commerce 2.2.3 I've created a way for the roles of buyers to add products to a cart and then save their cart. when their cart gets saved i've sent their cart back to the managers account. If he has all his buyers saved carts in his account. buyer A, buyer B, buyer C. let's say buyer A and buyer C have the same product in their cart. Well when i'm invoicing back to each buyers account how can I know which buyers had the same product if it just increments and merges.

I hope this makes sense and any help would be greatly appreciated. Thank you.

Best Answer

If every time want to add a separate product in cart, then you can pluginize representProduct method located in Magento\Quote\Model\Quote\Item

Your [Vendor]/[Module]/etc/di.xml looks like below

<?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\Quote\Model\Quote\Item'>
        <plugin name='beforeDispatch' type='[Vendor]\[Module]\Plugin\Model\Quote\ItemPlugin' sortOrder='99'/>
    </type>    
</config>

and your [Vendor]\[Module]\Plugin\Model\Quote\ItemPlugin.php looks like below

<?php

namespace [Vendor]\[Module]\Plugin\Model\Quote;
class ItemPlugin 
{
    
    public function afterRepresentProduct(\Magento\Quote\Model\Quote\Item $subject, $result)
    {
        if ($yourCondition) { // if there is no condition return always false
             $result = false;
        }
        return $result;
    }
}