0

Trying to add configurable product to cart with custom qty with below code but getting You need to choose options for your item error.

foreach($optionQty as $key => $qty) {
            $storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
            $productloaded = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($pid);

            if($qty > 0) {
                $params = [
                    'form_key' => $this->formKey->getFormKey(),
                    'product' => $productloaded->getId(),
                    'qty' => $qty,
                    'super_attribute' => [
                        $attributeId => $key
                    ],
                ];


                $productloaded->addCustomOption('additional_options', json_encode($additionalOptions));
                $this->cart->addProduct($productloaded, $params);

            }

        }
         $this->cart->save();
         $this->cart->getQuote()->setTotalsCollectedFlag(false)->collectTotals()->save();

Params array look like

    Array
(
    [form_key] => n6fGcdLmKgonfMRM
    [product] => 37226
    [qty] => 5
    [super_attribute] => Array
        (
            [135] => 105
        )

)

Can anybody help me with this

Thanks

  • You want to add configurable product to cart? – Suresh Chikani Aug 7 '18 at 11:27
  • yes configurable product – Daniel_12 Aug 7 '18 at 11:43
  • Which have two option like color and configurable Right? – Suresh Chikani Aug 7 '18 at 11:52
  • Yes am already passing it 'super_attribute' => [ $attributeId => $key ], – Daniel_12 Aug 7 '18 at 11:53
  • You have passed only single attribute, It required two attribute value. – Suresh Chikani Aug 7 '18 at 11:55

Your Answer

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

Browse other questions tagged or ask your own question.