Magento - How to get the subtotal for a single product in cart?

Magento - How to get the subtotal for a single product in cart?

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How to get the subtotal for a single product in cart?

Magento: Retrieving Single Product Subtotal in Cart

While Magento provides robust e-commerce functionality out of the box, sometimes you need to access specific data points like the subtotal for an individual product within the cart. This can be particularly useful for displaying detailed pricing information on the checkout page or for calculating shipping costs based on item totals.

Understanding the Current Approach and Limitations

The provided code attempts to retrieve the total price by summing up all items in the cart. Although this works, it doesn’t fulfill the specific requirement of getting the subtotal for a single product identified by its ID.

$totals = Mage::getSingleton("checkout/cart")->getQuote()->getTotals();
$subtotal = $totals["subtotal"]->getValue();
echo Mage::helper('checkout')->formatPrice($subtotal);

This script calculates the overall subtotal for all items in the cart, which is not what we need here.

Retrieving Subtotal for a Single Product

To achieve this, you’ll need to work with the Mage_Core_Model_Session object and specifically access the Singleton instance of the Magento shopping cart (Mage::getSingleton('checkout/cart')).

The key is to iterate through the items in the quote and find the one that matches your desired product ID.

Here’s a step-by-step guide:

  1. Retrieve the current shopping cart quote:
$cart = Mage::getModel('checkout/session')->getQuote();
  1. Get all items from the quote:
$items = $cart->getAllVisibleItems();
  1. Loop through each item and check if it matches your desired product ID:
foreach ($items as $item) {
    // Assuming you want to find an item with ID '1'
    if ($item->getProductId() == 1) {
        // Get the quantity of this particular item
        $qty = $item->getQty();

        // Calculate the single price (unit price)
        $singlePrice = $item->getProduct()->getFinalPrice();

        // Calculate the total for this item
        $total = $qty * $singlePrice;

        // Format and output the result
        echo "Quantity: {$qty}, Single Price: {$singlePrice}, Total: {$total}\n";
    }
}

Conclusion

Retrieving the subtotal for a single product in the Magento cart involves navigating the shopping cart session, accessing the quote object, and iterating through its items. By identifying your desired product using its ID and calculating the quantity multiplied by the unit price, you can achieve the required output.

Remember to replace ‘1’ with the actual ID of the product you’re interested in when running this code snippet.

This approach ensures that you have access to the detailed pricing information for individual products within the cart, enabling more granular control over your checkout process and customer experience.

For further insights on Magento development best practices and advanced customizations, feel free to visit our website at https://laravelcompany.com where we provide comprehensive guides and solutions tailored for Magento developers.

Checkout Catalog Pricing

Our network