2

i have written a small functionality, where i need to get data from XML or CSV and populate the data in the cart.

I am able to read the data from the file and create an array of the items

foreach ($products as $productId => $singleQty)
    {

        $product = $this->productModel->load($productId);
        if ($product)
        {
            echo $product->getId() .'<br/>';
            $this->cart->addProduct($_product, array('qty' => $singleQty));
            $this->cart->save();
        }
    }

the echo result is completely fine, it displays the product Ids correctly

But only one item gets added into the cart, with the total number of items.

Suppose i have product IDs 2 and 4 with quantities 200 and 400 respectively,

the above snippet only adds product with ID 2, but the quantity added is 600

Any idea what i am doing wrong?

2 Answers 2

2

I have been able to resolve it by removing the _productModel

foreach ($products as $productId => $singleQty)
    {
        $this->_cart->addProduct($productId, array('qty' => $singleQty));
    }
    $this->_cart->save();

This works fine

1

You just need to save cart outside the foreach loop

foreach ($products as $productId => $singleQty)
{

    $product = $this->productModel->load($productId);
    if ($product)
    {
        echo $product->getId() .'<br/>';
        $this->cart->addProduct($_product, array('qty' => $singleQty));

    }
}

$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.