Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I think this is a common requirement, when you've got prices like $5,300.00 most people don't want that trailing .00 thing, whereas if the price is $500.65 they do want the decimals.
We could easily put this as an extra option of the currency settings (even just in code) and then do something like this in commerce_currency_format()
$decimals = (floor($amount) == $amount) ? 0 : $currency['decimals'];
Comment | File | Size | Author |
---|---|---|---|
#5 | commerce-hide_decimals_if_zero-1596230-5.patch | 1.08 KB | bgilhome |
Comments
Comment #1
rszrama CreditAttribution: rszrama commentedAny reason we can't just direct people to http://drupal.org/project/commerce_extra_price_formatters? I wouldn't make this part of the currency format itself, as it's more of a case-by-case presentation issue.
Comment #2
pcambraThere's a slightly tiny minor detail in that module, that formatter overrides commerce_currency_format function and it just removes the decimal part no matter what, so both $500.00 and $500.15 would be displayed at $500.
I could patch extra price formatters if you feel that kind of setting doesn't belong to core but then we have the issue that you can't combine formatters so you can't use commerce_extra_price_formatters with commerce_price_savings or commerce_price_table
Comment #3
pcambraAlso, we have the issue with the order total views handler, which is not a field and we can't change anything there by UI.
Comment #4
rszrama CreditAttribution: rszrama commentedHmm, good point. That area handler uses the "Formatted with components" display formatter, but there's no reason we can't make it configurable.
Comment #5
bgilhome CreditAttribution: bgilhome commentedI've attached a patch to add a key 'decimals_zero' to the info array for each currency, value is boolean to enable showing/hiding decimals when they are zero.
Comment #6
philsward CreditAttribution: philsward commentedI tried the patch, but it did nothing for me...
Price formatting is VERY important to the psychology of purchasing.
Though the following blogs aren't what I would call "educational", studies have been done on the matter and there's a lot of info out on the web about price formatting. Keeping up with the best practices, no matter how uneducated the users of Drupal Commerce may be, is worth doing. A programmer is more than willing to stay complaint and keep up with best practices on metatags, or HTML5, or PHP despite the users not knowing a thing about those particular best practices, but it doesn't make sense to keep up on making Commerce work as an up-to-date shopping cart from a marketing standpoint? Just throwing it out there...
See:
Remove the Comma When Possible
Avoid Discounts With Precise Numbers
http://www.helpscout.net/blog/pricing-strategies/ - See Point #10
http://m.huffpost.com/us/entry/6182498 - towards the bottom
There's a lot more to prices than just displaying the numbers a certain way on a use-case basis. It needs to be looked at from a marketing standpoint, not a ux or code design standpoint.
If I could code, then sure, I'd go into the template.php and add the custom function pre_process thingy, but I don't code. I can't. Don't have the mind for it. How I can use and manage and navigate Drupal alone, is beyond me but that's the precise reason I USE Drupal: So I can have all of the power without ever touching the code.
Comment #7
szeidler CreditAttribution: szeidler at Ramsalt Lab commentedThe patch is working as expected for me.
@philsward. Did you configured your currency to hide the zero decimals? You need to set the property to false, clear the cache and it should work.
Comment #8
philsward CreditAttribution: philsward commentedthanks @szeidler. I tried the template.php function code, but couldn't get it to work. I ended up manually changing "'decimals_zero' => FALSE," inside the commerce.module file to get it to work.
From what I can tell, what you posted should work fine (USD for currency code) but it just wouldn't do it for me... Either I'm just that clueless or there's a piece missing. :-/
Comment #9
szeidler CreditAttribution: szeidler at Ramsalt Lab commentedI believe the alter code only works in a custom module, not template.php
Comment #10
philsward CreditAttribution: philsward commentedTake a look at: https://www.drupal.org/project/commerce_price_decimals_formatter
This allows a formatter setting of "Formatted amount with n decimals", and that allows the setting of "Sets zero decimal places when possible" which removes the decimal if .00
Not sure how it will affect other aspects of price calculations, but it does work for a basic price change.