Skip to main content
Removed horribly outdated practice of using underscores in variable names to denote visibility
Source Link

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$product = $this->_productModel>productModel->load($productId);
        if ($_product$product)
        {
            echo $_product$product->getId() .'<br/>';
            $this->_cart>cart->addProduct($_product, array('qty' => $singleQty));
            $this->_cart>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?

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?

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?

deleted 11 characters in body; edited tags
Source Link
Khoa TruongDinh
  • 32.3k
  • 11
  • 90
  • 156

i have written a small functionlityfunctionality, 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 itemsitem gets added into the cart, with the total number of items.

That is.

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?

i have written a small functionlity, 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 items gets added into the cart, with the total number of items.

That is.

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

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?

Source Link

Magento 2, Add Product in Cart in loop

i have written a small functionlity, 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 items gets added into the cart, with the total number of items.

That is.

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