5

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?

5

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.

  • Thanks Ronak! I was having exact same issue and get it resolved using above trick.. +1 (Y) – Himmat Paliwal Mar 6 '17 at 10:05
  • @HimmatPaliwal if This answer help you to resolve your issue then you can up-vote the answer – Ronak Chauhan Mar 6 '17 at 10:13
  • I already did that :) – Himmat Paliwal Mar 6 '17 at 10:47
  • Hi Ronak, i need to add multiple products with multiple sizes on the Category page at once. Can you help me? – kiran May 22 at 3:01
  • how can i help you @kiran – Ronak Chauhan May 23 at 8:06
0

In case anyone is using repository approach for loading the product and facing same issue as I did.

After injecting \Magento\Catalog\Api\ProductRepositoryInterface; into your class, use

$this->productRepository->getById($productId, false, $storeId, true);

the key here is the fourth parameter which "force reload" the object query and bypass the repository cache

in case anyone wondering how to get $storeId inject \Magento\Store\Model\StoreManagerInterface into your class and use

$storeId = $this->storeManager->getStore()->getId();

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.