Shopify 打算将任何转换后的价格四舍五入并显示为两位小数。但是,如果你想将它们四舍五入为整数(或任何小数位)而不带尾随零,可以在不使用任何应用程序的情况下快速解决这个问题。就性能而言,此 hack 是最快的,不会影响您的在线商店速度。
整数价
转到在线商店 > 操作 > 编辑代码。在您的price.liquid文件中,找到如下内容:
assign compare_at_price = target.compare_at_price
assign price = target.price | default: 1999
assign available = target.available | default: false
assign money_price = price | money
将其替换为:
assign compare_at_price = target.compare_at_price | divided_by: 100.0 | round | times: 100
assign price = target.price | default: 1999 | divided_by: 100.0 | round | times: 100
assign available = target.available | default: false
assign money_price = price | money_without_trailing_zeros
也更换 {{ compare_at_price | money }}
和 {{ compare_at_price | money_without_trailing_zeros }}
。
确保在主题设置中关闭显示货币代码,如 Shopify 的 “money_with_currency”过滤器在向外币添加货币代码时自动添加尾随 0。
将价格四舍五入到任何小数位
上述步骤显示四舍五入为整数。如果您想四舍五入为小数,请将前面代码块中出现的所有round
替换为round: n
,其中 n 是您要四舍五入到的小数位数(例如, round: 1
表示四舍五入到小数点后 1 位).
喜欢这篇博文吗?试用我们与您的主题集成的免费货币转换应用程序。
5 comments
hello, i cannot find my price.liquid file, is it the case that the file may not exit for some theme?
Thank you so much for that! I spent hours online looking for a way to do this, but it was time well spent once I found this post. Our products are over 100euro and I was really hating the .95 at the end of the converted prices! This worked perfectly, and my shop looks so much better now.
Hi,
I used your code and find it helpful to both display the simpler rounded price on the site, while charging the customer the true lower price. Thank you!
However, I hoped to removed the trailing zeroes while displaying the rounded price but I also need to show the currency code. Particularly as my shop is in Canadian dollars and I often sell in US dollars. Is this not truly not possible?
“Make sure to turn off Show currency codes in your theme settings, as Shopify’s “money_with_currency” filter auto-adds trailing 0s when adding currency codes to foreign currencies."