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?
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)
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.
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.
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:
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.
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.
// Secret Sauce - Initializes the Session for the FRONTEND // Magento uses different sessions for 'frontend' and 'adminhtml' Mage::getSingleton('core/session', array('name'=>'frontend'));
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.
// Secret Sauce - Initializes the Session for the FRONTEND // Magento uses different sessions for 'frontend' and 'adminhtml' Mage::getSingleton('core/session', array('name'=>'frontend'));
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
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?