跳到内容

Country

Language

Best way to round converted price on Shopify

在 Shopify 上四舍五入转换价格的最佳方式

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 位).

喜欢这篇博文吗?试用我们与您的主题集成的免费货币转换应用程序

Older Post
Newer Post

5 comments

  • Great post, thanks a lot.
    When I go to the checkout, the original unrounded price will show. Is there a possible way to change that?

    Best, Daniel

    Daniel
  • Thank you so much for this!

    Yoti

Leave a comment

关闭 (esc)

弹出窗口

使用此弹出窗口嵌入邮件列表注册表单。或者将其用作带有产品或页面链接的简单号召性用语。

年龄验证

单击 enter,即表示您已达到可以饮酒的年龄。

Search

购物车