Stripping Decimals from Prices for a Single Store in Magento
In Magento, the default behavior is to display prices with a decimal point based on the currency settings. While there are ways to remove decimals entirely or for all currencies globally, achieving this for only one store requires a more targeted approach. Let’s explore how to strip decimals from prices specifically for a single store view.
Understanding Magento’s Currency Configuration
Magento determines price formatting through its core Zend_Currency class, which is not easily overridden without affecting the entire site. However, if you’re willing to delve into the display layer of the codebase, there are more flexible options available.
The core file responsible for currency handling in Magento is app/code/core/Mage/Directory/Model/Currency.php. This file contains the logic that formats prices based on the currency settings in your system configuration.
Overriding Currency Format for a Single Store
To remove decimals from prices for only one store, we can override the _format() method in the Currency model. By doing so, we can apply a different formatting rule specifically for the desired store view.
Here’s how to achieve this:
- Create an extension or use your custom theme folder (app/design/frontend/{package}/{theme}/).
- Navigate to app/code/local or app/code/package/theme.
- Create the necessary directories: Mage/Directory/Model/Currency.php.
- Open a text editor and create a new file named Currency.php in the directory you just created.
Your override should look something like this:
<?php
class Mage_Directory_Model_Currency extends Mage_Core_Model_Abstract
{
public function format($price, $currency = null)
{
if (!is_numeric($price) || !is_string($currency)) {
return false;
}
// Check for the specific store view you want to apply the formatting to
$storeId = Mage::app()->getStore()->getId();
if ($storeId == 2 && $currency == 'USD') { // Change this line to match your desired store ID and currency
return number_format($price, 0);
}
return parent::format($price, $currency);
}
}
In the example above, replace the condition with the appropriate store ID (e.g., $storeId == 2) and currency code (e.g., 'USD') that you want to apply the decimal stripping to.
This override will ensure that only prices in the specified store view and for the designated currency are displayed without decimals. Any other store views or currencies will retain their original formatting rules.
Configuring Your Extension
To ensure your override works correctly, follow these steps:
- Flush Magento’s cache:
php -f shell/indexer.php --reindexallfrom the command line in the root directory of your Magento installation. - Log out and log back into the admin panel.
- Verify that prices are displayed without decimals for the specified store view.
Conclusion
Stripping decimals from prices for a single store in Magento requires an understanding of the core currency handling logic and the ability to override specific model methods within your extension or custom theme folder.
By following the steps outlined above, you can create a targeted solution that removes decimal points from prices only for the desired store view without affecting other store views on your site. This approach allows you to maintain the flexibility of having different formatting rules across multiple stores.