Magento

eCommerce Software for Online Growth

Magento Forum

From setting up your store to managing your products, pages and promotions to generating detailed reports, the Magento User Guide empowers the user to utilize the platform for all of its vast capacity.
Available in eBook and Print formats – Download Now!!!
   
Page 1 of 2
How to get items / total in the cart? Using Mage::getSingleton(’checkout/cart’) without any luck, always empty
 
One Step Checkout
Member
 
Total Posts:  33
Joined:  2008-03-21
Norway
 

Hello guys and girls,

I am trying to make a modified link to the shopping cart which displays an additional link if there are any products in the cart (go directly to checkout). I try to fetch data from the cart by using Mage::getSingleton(’checkout/cart’)->getItems(), but it ALWAYS returns an empty array, even though there are products in the cart.

I have also tried to use the helper Mage::helper(’checkout/cart’)->getCart() but no luck.

Does anyone know why the data I get is always empty, even though there are items in the cart?

Thanks in advance for any response!

Jone

 Signature 

Magento checkout - Norwegian restaurantguide

 
Magento Community Magento Community
Magento Community
Magento Community
 
One Step Checkout
Member
 
Total Posts:  33
Joined:  2008-03-21
Norway
 

Any chance I could get some help on this?

 Signature 

Magento checkout - Norwegian restaurantguide

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lee Saferite
Guru
 
Avatar
Total Posts:  312
Joined:  2007-08-31
Lake City, FL
 

Does Mage::helper(’checkout/cart’)->getItemsCount() return anything?

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
One Step Checkout
Member
 
Total Posts:  33
Joined:  2008-03-21
Norway
 

It always returns zero.

See this page, it always prints Mage::helper(’checkout/cart’)->getItemsCount() - http://ladiesroom.no/index-test.php

Then add some product in the cart here: http://www.ladiesroom.no/svart-klassisk-silketopp.html - and refresh the index-test.php url again. Still only 0 from getItemsCount()

 Signature 

Magento checkout - Norwegian restaurantguide

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  479
Joined:  2008-07-11
New Haven, CT
 

I believe you need to start the session if you are calling that function from a blank page and/or a page outside of Magento (for instance in your own module with your own controllers)

Check out files here for some code samples:

app/code/core/Mage/core/Model/Session.php

app/code/core/Mage/core/Model/Session/Abstract.php (and other files in that area)

 Signature 

@ My Magento Blog

- Handy tutorials on programming and designing Magento. A continual work in progress!
- Now with a new design, new hosting and a new domain! Check it out!

 
Magento Community Magento Community
Magento Community
Magento Community
 
piotrn
Member
 
Total Posts:  33
Joined:  2008-04-27
 

If you are using page that is not a part of Magento application, make sure that before you will start
using Magento’s models you will have a:

<?php
         
require_once("app/Mage.php");
     
Mage::app ();
?>
This will setup Magento enviroment, start proper session, create db connections ect.

 
Magento Community Magento Community
Magento Community
Magento Community
 
One Step Checkout
Member
 
Total Posts:  33
Joined:  2008-03-21
Norway
 

I have copied index.php to index-test.php and the code I’ve written here is tested in index-test.php.

Is there any session work I need to do then? The code is identical to index.php, just with doing Mage::app() instead of Mage::run(), so I can execute my code instead of dispatching to any Magento controller.

Any ideas on this?

 Signature 

Magento checkout - Norwegian restaurantguide

 
Magento Community Magento Community
Magento Community
Magento Community
 
piotrn
Member
 
Total Posts:  33
Joined:  2008-04-27
 

It should work, provided you are checking both pages with the same browser.
Session ID is transmitted with the page request, and using this ID,
PHP knows which session data it must use.
The Mage::app() sets up the session (informs php that it wants to use stored session data), so you shouldn’t need to do anything else.
At this point, the cart singleton should be registered and accessible for your script.

Check the web server logs, maybe there is a problem mentioned there.

Session can’t be restored, after any output is send to browser. Some editors put strange characters at the beginning of the files,
that are being transmitted to the browser just as the file starts. This can prevent session from being restored (you should see a warning in your log).

If that doesn’t help you,
You could paste here all the code you want to run, and explain once more (in detail) how you accessing it, so we can have a look.

 
Magento Community Magento Community
Magento Community
Magento Community
 
One Step Checkout
Member
 
Total Posts:  33
Joined:  2008-03-21
Norway
 

I’ve now tested this some more. There are some session stuff going on because I got an error about headers being sent when I tried to get something from the cart AFTER i had written to the output buffer.

However, I was unable to fix this problem. Here are the contents of my index-test.php:

<?php

$mageFilename 
'app/Mage.php';
require_once 
$mageFilename;

umask(0);
Mage::app();

// $cart = Mage::getSingleton('checkout/cart')->getItemsCount();
// $cart = Mage::helper('checkout/cart')->getItemsCount();
$cart Mage::helper('checkout/cart')->getCart()->getItemsCount();

echo 
'cart items count: ' $cart;

?>

As you can see it’s a copied index.php, I’ve removed the version checker as well as the downloader for readability. You can see the $cart = line here which tries to get the amount of items in the cart. Before the line you see two commented lines which are code that I’ve also tried.

This index-test.php can be reached here: http://www.ladiesroom.no/index-test.php - It always says “cart items count: 0”

The actual store is running on index.php as usual, and is available on http://www.ladiesroom.no - if you go to that URL, add a product in the cart, and then refreshes index-test.php, it still says “cart items count: 0”

It would be great if anyone could test my index-test.php and see if it works for them. I don’t know how to explain this any better, please ask if there is something I’ve forgot to add.

 Signature 

Magento checkout - Norwegian restaurantguide

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lee Saferite
Guru
 
Avatar
Total Posts:  312
Joined:  2007-08-31
Lake City, FL
 

Well, you are missing some secret souce.  Try this

<?php

$mageFilename 
'app/Mage.php';
require_once 
$mageFilename;

umask(0);
Mage::app();

// Secret Sauce - Initializes the Session for the FRONTEND
// Magento uses different sessions for 'frontend' and 'adminhtml'
Mage::getSingleton('core/session', array('name'=>'frontend'));

// $cart = Mage::getSingleton('checkout/cart')->getItemsCount();
// $cart = Mage::helper('checkout/cart')->getItemsCount();
$cart Mage::helper('checkout/cart')->getCart()->getItemsCount();

echo 
'cart items count: ' $cart;

?>

 Signature 

Lokey Coding, LLC
+1-386-269-0070

 
Magento Community Magento Community
Magento Community
Magento Community
 
Northern Voyageur
Member
 
Avatar
Total Posts:  49
Joined:  2008-06-05
Duluth, MN
 

Using this same setup how would I get a list of products that are in the customer’s cart?

 
Magento Community Magento Community
Magento Community
Magento Community
 
ShopGuy
Guru
 
Total Posts:  432
Joined:  2008-09-07
 

If anyone knows this I would appreciate it too

 Signature 

Monthly Clubs

 
Magento Community Magento Community
Magento Community
Magento Community
 
Northern Voyageur
Member
 
Avatar
Total Posts:  49
Joined:  2008-06-05
Duluth, MN
 

I finally figured it out.  For reference I used the file ‘app/code/core/Mage/GoogleCheckout/Model/Api/Xml/Checkout.php’ It has the fields of the cart that you might be looking to export.

<?php

$mageFilename 
'app/Mage.php';
require_once 
$mageFilename;

umask(0);
Mage::app();

// Secret Sauce - Initializes the Session for the FRONTEND
// Magento uses different sessions for 'frontend' and 'adminhtml'
Mage::getSingleton('core/session', array('name'=>'frontend'));

// $cart = Mage::getSingleton('checkout/cart')->getItemsCount();
// $cart = Mage::helper('checkout/cart')->getItemsCount();
// $cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
// echo 'cart items count: ' . $cart;

// $cart = Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal();
// echo 'Subtotal: ' . $cart;

$session Mage::getSingleton('checkout/session');

$output "";

foreach (
$session->getQuote()->getAllItems() as $item{
    
    $output 
.= $item->getSku() . "<br>";
    
$output .= $item->getName() . "<br>";
    
$output .= $item->getDescription() . "<br>";
    
$output .= $item->getQty() . "<br>";
    
$output .= $item->getBaseCalculationPrice() . "<br>";
    
$output .= "<br>";
}

print $output;

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  479
Joined:  2008-07-11
New Haven, CT
 

Very useful, thanks!

 Signature 

@ My Magento Blog

- Handy tutorials on programming and designing Magento. A continual work in progress!
- Now with a new design, new hosting and a new domain! Check it out!

 
Magento Community Magento Community
Magento Community
Magento Community
 
MacKniven
Member
 
Total Posts:  48
Joined:  2009-01-09
 

Would just like to thank everyone in this thread as you helped me solve a problem I had spent days on! Now I can export product details to my payment provider smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Frederik Krautwald
Member
 
Avatar
Total Posts:  52
Joined:  2007-09-25
Reykjavík, Iceland
 

Very useful post. However, I am trying on the product list page to display for each product item how many currently are in the cart. Does anyone know the solution to this?

Thanks in advance.

 Signature 

Icelandic Web Design and Web hosting

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
Page 1 of 2
 
© Copyright 2010 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
177719 users|1133 users currently online|277300 forum posts