Magento 2: Add to Cart One Product with Different Sizes as One Product

addtocartconfigurable-productmagento-2.1shopping-cart

I have created one module in which we can directly add products to cart using SKUs with different configuration options

enter image description here

but when i added it shows one product in cart with last option i selected and 2 qty.

enter image description here

I want to add same product with different options and quantities.

Proocess which generate an Array like this which i pass to store in cart :

Array(
[0] => Array(
        [WT09] => Array(
                [product] => 1817
                [sku] => WT09
                [qty] => 1
                [super_attribute] => Array
                    (
                        [141] => 167
                    )
            )
    )
[1] => Array(
        [WT09] => Array(
                [product] => 1817
                [sku] => WT09
                [qty] => 1
                [super_attribute] => Array
                    (
                        [141] => 171
                    )
            )
    ))

any idea on how to solve this issue?

Best Answer

Magento core module retrive Product object using

            $product = $this->_initProduct();

as it checks in cart session and retrive previous Item Id if matched so all need to do is remove that function from out module and retrive product using :

$this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($productId);

with this each time I'll get new Item Id and store it in session but if we already has same product with same configuration it will keep with same product and just append its quantity.

Related Topic