Magento Cart – Adding Multiple Products to Cart Failing

addtocartcart

Assuming I want to create a link for the customer to click on that automatically adds some items to cart and redirects them to the cart.

I created a controller that adds two items to cart, problem is, it's only adding the first item to cart, tried switching the order of the items and same problem.

Any ideas?

public function specialsAction(){
    $prod_one = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
        ->load($prod_one_id);
    $prod_two = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())
        ->load($prod_two_id);

    $params_one = array(
        'product' => $prod_one->getId(),
        'qty' => 1
    );

    $params_two = array(
        'product' => $prod_two->getId(),
        'qty' => 1
    );
    $cart = Mage::helper('checkout/cart')->getCart();

    // this item gets saved to cart, since its the first item i'm adding
    $cart->addProduct($prod_one, $params_one);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

    // this item does NOT get added (unless I add it first to the cart)
    $cart->addProduct($prod_two, $params_two);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

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

Answer

Since the answer below is a bit ambiguous I'm pasting the solution below:

    $cart->addProduct($prod_one, $params_one);
    // REMOVE these two lines and place them only at the end of all the product additions
    //$cart->save();
    //Mage::getSingleton('checkout/session')->setCartWasUpdated(true);


    $cart->addProduct($prod_two, $params_two);

    // HERE is where we save
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Best Answer

Try

...
$params_one = array(
    'product_id' => $prod_one->getId(),
    //^__change to product id
    'qty' => 1
);

$params_two = array(
    'product_id' => $prod_two->getId(),
    //^__change to product id
    'qty' => 1
);

$cart->addProduct($prod_one, $params_one);
$cart->addProduct($prod_two, $params_two);

$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

or

// assuming this is a simple product without option
$qty = 1;
$cart->addProduct($prod_one_id, $qty);
$cart->addProduct($prod_two_id, $qty);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

Read more Add Products to cart Programmatically in Magento

See /app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php

/**
 * Add product to current order quote
 * $product can be either product id or product model
 * $config can be either buyRequest config, or just qty
 *
 * @param   int|Mage_Catalog_Model_Product $product
 * @param   float|array|Varien_Object $config
 * @return  Mage_Adminhtml_Model_Sales_Order_Create
 */
public function addProduct($product, $config = 1)
{
    if (!is_array($config) && !($config instanceof Varien_Object)) {
        $config = array('qty' => $config);
    }
    ....
Related Topic