Home > Magento Tips > Get Product Id’s From Cart Session

Get Product Id’s From Cart Session

Posted on: 14th Jun 2011 By: Robert Kent 5 Comments

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. 1.4.2.0 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!

5 Responses to “ Get Product Id’s From Cart Session ”

  1. Ed Baxter
    #1 | 14th June 2011

    Cool little snippit, Thanks for sharing!

  2. Sunil Rana
    #2 | 8th November 2011

    I have created custom shopping cart popup by using some basic provided on blog, How to get all shopping cart items and total.

    http://www.phptechi.com/magento-how-to-get-all-shopping-cart-items-and-total.html

  3. Alessandro Cardinali
    #3 | 12th November 2011

    hello.
    How can I do to put this information in a form and send it by mail? as in: “Product Name Prefills Contact Form” http://www.e-commercewebdesign.co.uk/blog/magento-tutorials/product-name-prefills-contact-form.php

    tnx

  4. Yanith
    #4 | 8th December 2011

    i need to clear the abandoned carts, but where do i put this snippets?

  5. man_in_black
    #5 | 18th March 2012

    how to get product company address and company name ? what is the code like

    $_product->getShortDescription();

    what should i write instend of getShortDescription() ?

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.