Get Product Id’s From Cart Session
Here’s another quick and easy way of getting your cart data back from Magento. You may remember in a previous post that I used the checkout/session singleton to find out which attribute id your chosen products were from. 126.96.36.199 and later remove this function and require something like the following:
First of all re-define your session scope:
Mage::getSingleton('core/session', array('name' => 'frontend'));
Secondly go ahead and attach the cart model to a variable:
$cart = Mage::getModel('checkout/cart');
Now that we have the cart model we can go ahead and retrieve the following:
$ids = $cart->getProductIds(); $qty = $cart->getSummaryQty();
What the above will do is retrieve an array of all product ID’s that are in the cart and also a sum of all quantities in the cart. With these you can create your own little box which displays the number of products and also the total quantities of all items in your basket.
Another little trick while we are on the subject of carts is if you wish to create your own “clear cart” button this why not add this little piece of code:
Mage::getSingleton('core/session', array('name'=>'frontend')); Mage::getSingleton('checkout/session')->clear();
That’s all for today – hope you found it useful in some way – what I’ve found is it’s always good to write down these little snippets of code – it will save you hours of hunting for the same thing further down the road. Thanks for visiting the Magento Blog here at E-commerce Web Design – home of the Magento Fox!