how to get last item from cart magento

how to get last item from cart magento

September 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
how to get last item from cart magento

Retrieving the Last Item from Magento 2 Cart: Step-by-Step Guide

As a senior developer specializing in e-commerce platforms, I frequently encounter questions regarding cart management in Magento 2. One common query is how to retrieve specific details of the last item added to a user’s shopping cart.

Understanding the Magento 2 Cart Structure

Magento 2 uses a robust shopping cart system that stores all added items and their associated data for the current session. The Mage::getSingleton('checkout/session') method retrieves the active checkout session, which contains the quote ($session->getQuote()).

The quote object contains an array of items ($session->getQuote()->getAllItems()), each representing a single product in the cart.

Retrieving Basic Item Details

To retrieve basic details like item ID, name, and quantity for all cart items using Magento 2’s built-in methods, you can use the following code:

$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
    $productId = $item->getProductId();
    $productName = $item->getName();
    $productQty = $item->getQty();
}

Accessing the Product Object

To access more advanced details like image and URL for a product, you need to retrieve the associated product object. Magento 2 provides convenient methods to achieve this:

$session = Mage::getSingleton('checkout/session');
foreach ($session->getQuote()->getAllItems() as $item) {
    $product = $item->getProduct();
    $imageUrl = $this->helper('Magento\Catalog\Helper\Image')->init($product, 'category_page_list')
        ->setWidth(300)->setHeight(400)->getUrl();
    $url = $product->getProductUrl();
    $price = $product->getPrice();
}

In the above example, we use Magento’s catalog helper to fetch the product image URL with custom dimensions. The getProductUrl() method retrieves the product’s URL, and getPrice() fetches its price.

Getting the Last Item in the Cart

To retrieve information about the last item added to the cart, you can utilize PHP’s array reverse function:

$session = Mage::getSingleton('checkout/session');
$lastItem = end($session->getQuote()->getAllItems());
if ($lastItem) {
    $product = $lastItem->getProduct();
    $imageUrl = $this->helper('Magento\Catalog\Helper\Image')->init($product, 'category_page_list')
        ->setWidth(300)->setHeight(400)->getUrl();
    $url = $product->getProductUrl();
    $price = $product->getPrice();
}

Best Practices for Magento 2 Cart Management

When working with the cart in Magento 2, consider these best practices:

  1. Use ObjectManager: While the above examples use the Mage::getSingleton() method (available in Magento 1), Magento 2 recommends using the ObjectManager.

  2. Avoid Replacing Core Functions: Be cautious when overriding or replacing core functions related to cart management, as this can lead to compatibility issues.

  3. Optimize Performance: If you need to retrieve data from multiple item iterations, consider optimizing your code for better performance, especially in production environments.

Conclusion

Retrieving the last item from a Magento 2 shopping cart is a straightforward process that involves accessing the quote items and using built-in methods to fetch product details like image URL and price. By following these steps and best practices, you can effectively manage and display product information within your custom modules or themes.

For more advanced customizations and troubleshooting in Magento 2 development, I recommend exploring our comprehensive resources at Laravel Company to enhance your e-commerce expertise.

Magento 2 Checkout Catalog Database

Our network