5

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?

1 Answer 1

7

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.

1
  • Awesome. Thank you. I'll try to replace then my "addItems/removeItems to/from Quote" pieces of code with "addProduct/removeProduct to/from cart" ones...
    – nano
    Mar 23, 2016 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.