Magento – Adding multiple items with different attributes to cart programmatically

addtocartattributescartproductprogrammatically

I'm making a bulk add to cart system.
Please note : i want it to works for simple products with with custom options -> Where custom option are like color(red , green , blue) or Size(Xl, M , S)

Suppose a person wants to order below items:

  1. product A, red color, qty 12
  2. Product A, green color, qty 18
  3. Product B, XL, qty 3
  4. Product C, qty 10

So I want to add these 4 items by code/programmatically at once. How can I do this? Is it possible via query string, or any controller or built in function for that? It doesn't have to be a single query or one function call only per see…

Best Answer

So to add products to cart programmatically is fairly simple you just need to product object and the cart session.

$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($product, $qty);

$quote->collectTotals()->save();

This because a bit more difficult when adding configurable or products with options but all you need to do is load the product object with the right options.

Now what you need to do is pass an array instead of $qty and this array should be formatted in a different way depending on what type of product you are adding.

See the following for more information:

Related Topic