0

In my magento, I have added a product to cart - so I see the following on the checkout page:

enter image description here

I have a custom module with a TestController with a IndexAction where I want to retrieve the current contents of basket for custom processing. This will be used to provide a "get delivery cost estimate" type functionality.

I.e. customer will add bunch of items into basket (as guest), click a button on front end and it takes them to our module where they will enter the destination country and postcode and we will do some custom processing to let the customer know what the estimated delivery cost is going to be.

I tried to retrieve the basket contents like this:

$cart_contents = Mage::helper('checkout')->getQuote();
foreach ($cart_contents->getItemsCollection() as $item) {
   var_dump($item->getName());
}
exit;

This does not return anything.

I also tried a different approach:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
echo "<pre>";
foreach ($cartItems as $item) {
    print_r($item->getData());
}
exit;

This is also returning nothing.

Bear in mind; this is a guest checkout (i.e. customer is not logged in).

To test there is a valid quote; I did the following:

echo "<pre>";
print_r(Mage::helper('checkout')->getQuote()->getData());
echo "<hr>";
print_r(Mage::getSingleton('checkout/session')->getQuote()->getData());
exit;

This is what I get back:

Array
(
    [store_id] => 5
    [is_checkout_cart] => 1
    [remote_ip] => 90.xxx.xxx.xxx
    [x_forwarded_for] => 90.xxx.xxx.xxx
)
Array
(
    [store_id] => 5
    [is_checkout_cart] => 1
    [remote_ip] => 90.xxx.xxx.xxx
    [x_forwarded_for] => 90.xxx.xxx.xxx
)
0

tested code try it

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

  if($cart->getAllItems()):

          foreach ($cart->getAllItems() as $item):
           echo $item->getProduct()->getName();
           endforeach;
 endif;
0

Use the model of the class called Mage_Sales_Model_Checkout_Cart.

This is the standard model of the shopping cart that works equally fine both for registered and non-registered users.

Here is the example of the code: https://www.gyazo.com/55a4f2d490e2573af8c25ccba2195ef8

This is just a shopping cart: https://www.gyazo.com/c23cc7d32598f847a785f7689b1f01bf

And this is the result of the code implementation: https://www.gyazo.com/7151712c0af8ff98cf178e6bf94c7a45

  • Okay, let me try your method also using Mage_Sales_Model_Checkout_Cart – Latheesan Mar 15 '16 at 15:52
  • 1
    Please don't post screenshots with the code. Post the actual code. – Marius Mar 15 '16 at 15:53
  • Actually never mind, this method is not any different from what Qaisar Satti suggested. I have tried this method also and it's returning nothing. – Latheesan Mar 15 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.