0

I am trying to add a configurable product with with different variants but its showing one item with 2 quantity instead of showing 2 item with each quantity one.

$wholedata = $this->getRequest()->getParams();

    $productId =$wholedata['product_id'];
    $superattribute = array();  
    foreach ($wholedata['qty'] as $eachkey => $eachparam) {
        foreach ($wholedata['config'] as $key => $value) {
            $superattribute[$key] = $value[$eachkey];
        }
        $params = array(
            'form_key' => $this->formKey->getFormKey(),
            //'product' => $productId, //product Id
            'qty'   =>$eachparam,
            'selected_configurable_option' => '',
            'super_attribute' => $superattribute        
        );     
        $_product = $this->product->load($productId);       
        $this->cart->addProduct($productId, $params);
        $this->cart->save();
    }

But Strange when I added it one by one and just refresh the cart page in-between then it works fine If have tried to use $quote->collectTotals()->save(); as well $checkoutsession->getQuote()->setCartWasUpdated(true); but nothing works. Please help

0

I Found a solution i.e

$wholedata = $this->getRequest()->getParams();

$productId =$wholedata['product_id'];
$superattribute = array();  
foreach ($wholedata['qty'] as $eachkey => $eachparam) {
    foreach ($wholedata['config'] as $key => $value) {
        $superattribute[$key] = $value[$eachkey];
    }
    $params = array(
        'form_key' => $this->formKey->getFormKey(),
        //'product' => $productId, //product Id
        'qty'   =>$eachparam,
        'selected_configurable_option' => '',
        'super_attribute' => $superattribute        
    );     
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
        $storeId = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
        $product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($productId);  
        $this->cart->addProduct($product, $params);
    $this->cart->save();
}

Works for me

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.