简体   繁体   English

Shopify / Cart.js /更新分配和计算的变量?

[英]Shopify / Cart.js / Update assigned and calculated variables?

Trying to calculate some values dynamically, but got stuck. 试图动态计算一些值,但被卡住了。 Would love to calculate tax amounts and display them. 很想计算税额并显示它们。 Before cart.js we assigned temporary variables which worked perfectly well. 在cart.js之前,我们分配了效果很好的临时变量。

Needless to say those need to be updated now as well. 不用说,现在也需要更新这些内容。

<div class="grid__item">
    {% assign var_net = cart.total_price | divided_by:1.19 %}   
    {% assign var_tax = cart.total_price | minus: var_net %}   
    <p>
        {{ cart.total_price | money_without_currency }} EUR<br/>
        —<br/>
        {{ var_net | money_without_currency }} EUR<br/>
        {{ var_tax | money_without_currency }} EUR<br/>                
        —<br/>
        {{ cart.total_price | money_without_currency }} EUR
    </p>
</div>

This draws the price: 这样得出价格:

<span rv-html="cart.total_price"></span>

But how can we make the above calculation? 但是,我们如何进行上述计算呢?

First off, you're going to want to make sure you're using the version of Cart.js that bundles Rivets.js (it comes as rivets-cartjs.min.js in Cart.js package). 首先,您将要确保使用的是捆绑Rivets.js的Cart.js版本(它在Cart.js软件包中为rivets-cartjs.min.js )。

You're then able to use a number of filters in your theme templates in a very similar way to Liquid - for example: 然后,您就能够在一个非常类似的方式液使用大量的过滤器在你的主题模板 - 例如:

<div class="grid__item" data-cart-view>
  <p>
    <span rv-text="cart.total_price | money_without_currency">
      {{ cart.total_price | money_without_currency }}
    </span> EUR
  </p>
</div>

This will both render the total price on initial page load with Liquid, and keep the text of the <span> dynamically updated. 这将在Liquid初始页面加载时呈现总价格,并保持<span>的文本动态更新。 Note the addition of the data-cart-view attribute, which tells Cart.js to dynamically update the contents of this element as the cart changes. 注意增加了data-cart-view属性,该属性指示Cart.js在购物车更改时动态更新此元素的内容。

Implementing your var_net and var_tax dynamically will be a little more tricky. 动态地实现var_netvar_tax会有些棘手。 Cart.js does ship with math filters, but only plus and minus and they assume you're working only with integers (I just opened a ticket to fix that in the next release). Cart.js确实与数学滤波器的船,但只plusminus ,他们假设你用整数唯一的工作(我只是开了票来解决,在未来的版本)。

Fortunately, we can define our own formatters for Cart.js really simply! 幸运的是,我们可以非常简单地为Cart.js定义自己的格式化程序! Try adding some code like this, after loading rivets-cart.min.js but before calling CartJS.init() : 在加载rivets-cart.min.js但在调用CartJS.init()之前,尝试添加这样的代码:

<script>
  rivets.formatters.var_net = function(price) {
    return price / 1.19;
  }
  rivets.formatters.var_tax = function(price) {
    return price - rivets.formatters.var_net(price);
  }
</script>

After doing that, you should be able to achieve both an accurate initial render in your Liquid code and keep things updated dynamically with a template like this: 之后,您应该能够在Liquid代码中实现准确的初始渲染,并使用如下模板来动态地更新内容:

<div class="grid__item" data-cart-view>
  {% assign var_net = cart.total_price | divided_by:1.19 %}   
  {% assign var_tax = cart.total_price | minus: var_net %}   
  <p>
    <span rv-text="cart.total_price | money_without_currency">
      {{ cart.total_price | money_without_currency }}
    </span> EUR<br/>
    —<br/>
    <span rv-text="cart.total_price | var_net | money_without_currency">
      {{ var_net | money_without_currency }}
    </span> EUR<br/>
    <span rv-text="cart.total_price | var_tax | money_without_currency">
      {{ var_tax | money_without_currency }}
    </span> EUR<br/>
    —<br/>
    <span rv-text="cart.total_price | money_without_currency">
      {{ cart.total_price | money_without_currency }}
    </span> EUR<br/>
  </p>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM