Magento – Get shopping cart details in Magento

magento

I want to get the shopping cart details, by using Magento's getQuote function. How can I do this?

$cart = Mage::getModel('checkout/cart')->getQuote();

When I print the $cart Page stops execution and blank page is shown.
But when I write

$cart = Mage::getModel('checkout/cart')->getQuote()->getData();

and print the $cart an array will show. But I want to track the complete cart data (product Id,Product price like all information).

Is there any other method by which I can find the shopping card data?

Best Answer

The object returned by getQuote is a Mage_Sales_Model_Quote. It has a method getAllItems which in turn returns a collection of Mage_Sales_Model_Quote_Item objects.

All this means you can inspect products like this:

$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllItems() as $item) {
    $productId = $item->getProduct()->getId();
    $productPrice = $item->getProduct()->getPrice();
}

PS. The reason you get a blank page is because dumping a whole object likely fell into recursion and the page timed out, or PHP ran out of memory. Using getData or debug is safer but, as you saw, doesn't return the protected/private variables.

Related Topic