How do I get items in a Magento shopping cart using models?

How do I get items in a Magento shopping cart using models?

July 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I get items in a Magento shopping cart using models?

Fetching Items From Magento Shopping Cart Using Models

In Magento, you can retrieve items added to the shopping cart using various models or helpers provided by the framework. Here are two common methods to achieve this:

Using Mage::getSingleton(‘checkout/session’) Model

The Mage::getSingleton('checkout/session') model allows you to access the current customer’s quote (shopping cart) and retrieve its items. You can use it in your controller or helper as follows:

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

// Get the quote (cart) for the current customer
$quote = $session->getQuote();

// Loop through each item in the cart
foreach ($quote->getAllItems() as $item) {
    // Print the product name and quantity
    echo 'Product: '.$item->getName().', Quantity: '.$item->getQty();
}

Using Mage::getModel(‘checkout/cart’) Model

The Mage::getModel('checkout/cart') model represents the cart object itself. You can use it to fetch all items in the cart for any customer, including guests.

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

// Get all items in the cart
$items = $cart->getQuote()->getAllItems();

// Loop through each item
foreach ($items as $item) {
    // Print product details and quantity
    echo 'Product ID: '.$item->getId().', Name: '.$item->getName().
        ', Quantity: '.$item->getQty();
}

Tips for Fetching Cart Items

  1. Always ensure that you are fetching the cart items within a controller or helper context, as Magento’s session and cart models require proper initialization.

  2. If you need to fetch the shopping cart for another customer, make sure to set the appropriate customer session before calling $session->getQuote().

  3. Use Mage::getSingleton('checkout/session')->clear() if you want to clear the shopping cart after fetching its items.

Conclusion

Magento provides several ways to fetch items from a shopping cart using models and helpers. The Mage::getSingleton('checkout/session') model is typically used in controller actions that deal with customer-specific carts, while Mage::getModel('checkout/cart') can be useful for handling general cart operations.

Remember to always ensure proper context and initialization when working with Magento’s session and cart models. This will help you avoid common issues like “session not set” errors or incorrect data retrieval.

For more advanced Magento customization, consider exploring the extensive resources available at Laravel Company. Our expert team can guide you through complex projects and deliver optimal solutions tailored to your business needs.

Checkout Catalog Customers

Our network