问题描述
警告:我不是程序员。我设法做的很多事情都是通过研究,反复试验。
我在我的网站平台上使用Shopify,超过50美元的订单提供免费送货。我在购物车页面上创建了一个非常基本的代码,告诉客户距免费送货有多近:
{% if cart.total_price >= 5000 %}
<div class="shipping-notice">You'll receive FREE SHIPPING to the contiguous US & Canada!</div>
{% elsif cart.total_price < 5000 %}
<div class="shipping-notice">You're only {{ 5000 | minus: cart.total_price | money }} away from FREE SHIPPING to the contiguous US & Canada!</div>
{% endif %}
最近,我安装了一个货币转换器应用程序,消息中的数字混乱了,因为它只是将5000
视为数字而不是美元值。 (也就是说,购物车中有38英镑的人还剩下12.00英镑可以免运费)
如何更改代码,以便在将cart.total_price值转换为USD之前将其应用于代码?
首选简单的术语。如我所说,我不是程序员。
解决方法
您可以计算视图中需要多少钱,并将其作为变量传递到模板。然后检查购物车中的金额是否在该值附近。
即:
def get(self,request):
cart = 1.00
free_shipping = 5000
if "Great Britain":
free_shipping = free_shipping * float(1 / 0.75)
else:
pass
return render(request,'template.html',{'free_shipping' : free_shipping,'cart' : cart})
{% if cart.total_price >= free_shipping %}
{% endif %}