Removing the Tag from formatPrice in Magento
In your question, you’re trying to remove the <span class="price"> tag from the formatPrice function used in a Magento checkout context without modifying core files directly. This can be achieved by using the currency helper method with some adjustments.
Understanding the Problem
The formatPrice function in Magento’s checkout helper (Mage_Checkout_Helper_Data) uses the currency function internally to format prices. The <span class="price"> tag is added mainly for styling purposes, using CSS classes defined in your theme. By default, it sets the currency symbol and applies some formatting rules.
Removing the Tag
You’ve already discovered that passing false as the third argument to Mage_Core_Helper_Data::currency() removes the <span class="price"> tag:
<?php echo $_coreHelper->currency($_finalPrice, true, false) ?>
This happens because the third parameter is called $showCurrencySymbol. When set to false, it prevents the currency symbol from being wrapped in a span with a class name.
Applying This to Other Function Calls
Now, let’s see how you can apply this discovery to your original problem. Your goal is to remove the <span class="price"> tag from the following call:
<?php echo $this->helper('checkout')->formatPrice($_item->getCalculationPrice()) ?>
Step 1: Understanding the Helper Function
The formatPrice function in Magento’s checkout helper is defined like this (simplified for brevity):
public function formatPrice($price, $showSign = false)
{
return $this->_currency->currency($price, true, $showSign);
}
It simply calls _currency->currency() and passes the $showSign argument directly to it.
Step 2: Using the currency Function Directly
Since you want to control the output of the formatPrice function without modifying its code, you can use a workaround by calling the currency function directly. Here’s how:
<?php echo $this->helper('checkout')->getCurrency()->currency($_item->getCalculationPrice(), true, false) ?>
How This Works
The getCurrency() method returns an instance of the currency model (Mage_Directory_Model_Currency). By calling its currency() method directly, we bypass the formatPrice function and have more control over the output.
Benefits of This Approach
This approach has several advantages:
- It avoids modifying core files, which is generally considered good practice.
- It’s flexible, allowing you to use different formatting options without changing the original function.
- It provides a clear separation between the currency formatting logic and its usage in your template.
Conclusion
In this post, we’ve explored how to remove the <span class="price"> tag from Magento’s formatPrice function without modifying core files. By understanding the flow of the helper functions and using the currency method directly with the desired parameters, you can achieve the output you want while keeping your code clean and maintainable.
Remember that the currency function provides other formatting options as well. For example, you can set the $showCurrencySymbol parameter to 'code' if you want to display the currency code instead of the symbol:
<?php echo $this->helper('checkout')->getCurrency()->currency($_item->getCalculationPrice(), true, 'code') ?>
This gives you more control over how prices are displayed in your Magento store without touching any core files.
If you found this guide helpful, feel free to explore our other tutorials on Magento development and e-commerce at https://laravelcompany.com/magento-development/. We cover a wide range of topics from theme customization to advanced extension development.