Magento - Using getPriceHtml on custom product collection doesn't return correct tax price

Magento - Using getPriceHtml on custom product collection doesn't return correct tax price

October 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Using getPriceHtml on custom product collection doesn't return correct tax price

Understanding Magento Product Pricing Behavior

The Initial Issue: Inconsistent Tax Pricing on Custom Collections

When working with custom product collections in Magento, you might encounter an issue where the getPriceHtml method doesn’t return correct tax prices when the store is set to display inclusive taxes.

Let’s first examine your initial setup. You’ve created a product collection using the resource model:

$_testproductCollection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->setPageSize(6);
$_testproductCollection->load();

You then iterate through this collection to display the prices:

foreach($_testproductCollection as $_testproduct){
    echo "Price is ".$this->htmlEscape($this->getPriceHtml($_product, true))."<br/>";
};

This works fine until you enable tax pricing. Instead of showing two separate prices (excl. and incl. tax), it displays the same price for both.

The Intriguing Behavior: getLoadedProductCollection

Interestingly, if you add a call to getLoadedProductCollection() immediately after loading your custom collection, the tax pricing issue is resolved:

$_productCollection=$this->getLoadedProductCollection();

However, this approach has a significant downside—it negates the page size limitation set earlier with setPageSize(6). Instead of returning 6 products, it returns the entire product collection for your store.

The Core Issue: How getLoadedProductCollection Works

The issue lies in how Magento manages tax calculations. When you create a custom collection, Magento doesn’t immediately apply all necessary filters and adjustments needed for displaying prices correctly with taxes. It only does so when the collection is actually rendered or accessed (like when you call getPriceHtml).

On the other hand, getLoadedProductCollection() essentially forces a re-render of your collection, ensuring that it has been fully processed by Magento’s pricing system.

The Elegant Solution: Using getPrices()

To resolve this issue gracefully without compromising pagination and performance, you can directly retrieve the tax prices using the product object’s getPrice() method. This approach skips the need for calling getLoadedProductCollection().

Here’s how to modify your loop:

foreach($_testproductCollection as $_testproduct){
    $regularPrice = $_testproduct->getPrice();
    $taxPrice = Mage::helper('tax')->getPrice($_testproduct, $regularPrice);
    echo "Regular Price: ".$this->htmlEscape($regularPrice)."<br/>";
    echo "Tax Price: ".$this->htmlEscape($taxPrice)."<br/><br/>";
};

In this code:

  • getPrice() retrieves the regular price.
  • Mage::helper(‘tax’)->getPrice($_testproduct, $regularPrice) calculates and returns the tax-inclusive price.

Conclusion

Understanding Magento’s pricing system is crucial for working with custom product collections effectively. While the initial behavior might seem counterintuitive, it’s important to remember that Magento follows a specific lifecycle when dealing with tax calculations and collection processing.

By leveraging the getPrice() method directly, you can ensure your prices are displayed correctly without compromising pagination or performance. This approach is more efficient than relying on getLoadedProductCollection(), which might lead to rendering unnecessary products if not managed carefully.

Remember, Magento’s pricing system is highly flexible and customizable. As a developer, it pays to understand its intricacies so you can build robust, high-performing e-commerce platforms. For more insights into Magento development best practices, feel free to explore our resources at Laravel Company.

Catalog API Pricing Errors

Our network