0

In my custom module, products are going to add directly to cart as below :

    foreach($productArray as $key => $id ) 
    {

    $p["qty"] = $qtyArray[$key]; 
    try {
                    $params = array(
                        'product' => $id,
                        'qty' => $p
                    );
                    $_product = $this->_productRepository->getById($id);
                    $this->cart->addProduct($_product,$params);
                    $this->cart->save();                            

        } catch(\Exception $e) { 
           $responseData['action']="errmessage";
            $responseData['msg'] = __("Something went wrong. please correct the sku(s)");
            $error = true;
        }
    }

A single product is added in the cart without issue but when we try to add multiple products together into the cart then the price of the first product is updated and subtotal is also updated but prices for 2nd individual products is showing 0 Any help will be appreciated! Thanks

2
  • Above code is to add single product, please provide code that you used to add multiple products to cart. Commented Jun 19, 2017 at 9:01
  • Please check updated code
    – Laty
    Commented Jun 19, 2017 at 9:17

1 Answer 1

1

After debugging, I have found the solution , $this->cart->save(); must be after the loop. Check below updated code:

 foreach($productArray as $key => $id ) 
    {

    $p["qty"] = $qtyArray[$key]; 
    try {
                    $params = array(
                        'product' => $id,
                        'qty' => $p
                    );
                    $_product = $this->_productRepository->getById($id);
                    $this->cart->addProduct($_product,$params);                                               

        } catch(\Exception $e) { 
           $responseData['action']="errmessage";
            $responseData['msg'] = __("Something went wrong. please correct the sku(s)");
            $error = true;
        }
    }
$this->cart->save(); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.