Magento - Adding products to cart using a loop

Magento - Adding products to cart using a loop

August 19, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Adding products to cart using a loop

Magento: Adding Products to Cart Using a Loop

Are you trying to add multiple products to your Magento cart using an external request containing product IDs? You’re on the right track! I’ll guide you through the process and help you resolve the issue with duplicate items in your cart.

Understanding the Current Approach

You’ve already implemented some great logic:

  1. You extract the product IDs from the external request.
  2. Initialize the shopping cart.
  3. Load each product using a loop, logging their IDs for debugging purposes.
  4. Attempt to add each product to the cart with a quantity of 1.

Here’s your current code snippet:

public function indexAction() {
    $ids = explode(',', $this->getRequest()->getParam('products'));

    Mage::log('ADDING PRODUCTS');

    $cart = Mage::getModel('checkout/cart');
    $cart->init();

    $pModel = Mage::getSingleton('catalog/product');

    // Disable this line for now
    //$cart->addProductsByIDs($ids);

    foreach ($ids as $id) {
        Mage::log('Loading: ' . $id);
        $product = $pModel->load($id);
        Mage::log('Loaded: ' . $product->getId());

        try {
            $cart->addProduct($product, array('qty' => '1'));
        } catch (Exception $e) {
            Mage::log($e);
            continue;
        }
    }

    $cart->save();

    if ($this->getRequest()->isXmlHttpRequest()) {
        exit('1');
    }

    $this->_redirect('checkout/cart');
}

You’ve observed that this approach leads to duplicate items in the cart, with one product missing after the redirection. This is because save() is called before the redirection.

Resolving the Duplicate Item Issue

To fix this issue without losing control over the quantity of products:

  1. Move the $cart->save() line inside the loop, just after adding each product.
  2. Remove the $cart->init() call since it’s not needed here.

Here’s the updated code snippet with these changes:

public function indexAction() {
    $ids = explode(',', $this->getRequest()->getParam('products'));

    Mage::log('ADDING PRODUCTS');

    $pModel = Mage::getSingleton('catalog/product');

    foreach ($ids as $id) {
        Mage::log('Loading: ' . $id);
        $product = $pModel->load($id);
        Mage::log('Loaded: ' . $product->getId());

        try {
            // Save the cart after adding each product
            $cart = Mage::getModel('checkout/cart');
            $cart->addProduct($product, array('qty' => '1'));
            $cart->save();
        } catch (Exception $e) {
            Mage::log($e);
            continue;
        }
    }

    if ($this->getRequest()->isXmlHttpRequest()) {
        exit('1');
    }

    $this->_redirect('checkout/cart');
}

Conclusion

By moving the $cart->save() call inside the loop and removing unnecessary lines, you can now add multiple products to your Magento cart from an external request without experiencing duplicate items or losing control over product quantities.

Remember, it’s always important to test your code thoroughly after making changes like these. Monitor your system logs for any errors or unexpected behavior.

For more advanced techniques in Magento development and e-commerce solutions, don’t forget to check out Laravel Company, where we provide expert insights on various aspects of the Magento ecosystem.

Checkout Catalog Errors

Our network