Magento 1.9 Checkout – Quote vs Cart

cartcheckoutmagento-1.9onepage-checkoutquote

I want to add dynamically new products in the cart during the checkout process, but I've no clear the differences between the Quote and the Cart.

In my catalog pages I'm adding contents directly to the cart with:

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$cart->addProduct($product, $params);
$cart->save();

whereas in the checkout process I am using:

$quote = $this->getOnepage()->getQuote();
$product = Mage::getModel('catalog/product')->load(
    $params = array(
        'product' => 113,
        'qty' => 1
    );
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty(1);
$quoteItem->setStoreId(Mage::app()->getStore()->getId());
$quote->addItem($quoteItem); 
$quote->save();

Not sure why I am doing like that 🙂 Does it make sense to also add the products in the cart in that last step?

Can someone throw some light on this?

Best Answer

The cart model is an abstraction for the quote (or better: for the items in the cart) and you should use it wherever possible. Working with the quote object directly when it comes to adding, removing and changing items is complicated and almost never necessary.

As I've written in Programmatically create an order of grouped and bundle product in Magento:

Don't use Mage_Sales_Model_Quote::addProduct(), especially for complex products. Use Mage_Checkout_Model_Cart::addProduct() which is also used by the add to cart controller action and takes care for configuring the product and quote item. It takes a product id and the "request info" as parameters, where the request info can be simply the qty to buy (for simple products) or data for a buyRequest object. This is basically the $_POST data from an add to cart action.

Related Topic