Sunday, September 25

How to Add Multiple Products To The Cart Simultaneously

Adding Multiple Products To The Cart Simultaneously

In a previous post I looked at creating a product on the fly and adding it to the cart automatically. However, if you are using Magento without the catalog then when you transfer customers from your catalog to the cart and checkout you may need to create and add multiple products to the cart.

Creating multiple products is fairly straightforward if you use my methodology for creating a single product from the previous post. To add multiple products to the cart, create the products and add the product objects to an array, here I have named it $products.

To then add all of these products to the cart simultaneously use the following code:

<?php
equire_once (‘app/Mage.php’);
Mage::app();
$session = Mage:getModel(‘core/session’, array(‘name’ => ‘frontend’);

// create products here and add the objects to the array $products
$ids = array();

foreach ($products as $product)
{

$ids[] = $product->getID();

}

$cart = Mage::getModel(‘checkout/cart’);
$cart->addProductsByIDs($ids);
$cart->save();

// change to relevant URL for your store!
header(“location: http://localhost/magento/checkout/cart/”);
?>

Hope it Helps... Thanks...

No comments:

Post a Comment