簡體   English   中英

如何在購物車 shopify 上顯示不同的總數

[英]How to show diferent total on cart shopify

我根據itemquantitycart顯示不同的prices ,如果cart中的itemitem.quantity中超過6 ,它應該計算元metafield中保存的price ,否則顯示default price 知道這一點后,我想根據這些prices顯示cart中的total ,例如

//i declare a variable for the subtotal
{% assign subtotal = 0 &}
{% item.quantity >= 6 %}
    //if the item qty is greater than 6 i show a different prices, in this case a price saved on a metafield
    {% subtotal += item.product.metafields.global.wholesale_price | times:item.quantity %}
{% else %}
    {% subtotal+= item.price | times:item.quantity %}
{% endif %}

最后顯示小計

{{ 'general.cart_info.sub_total' | t }}<span><span class="amount">{{ subtotal | money }}</span></span>

正如我所看到的,不支持subtotal += value或者我使用錯誤,有沒有辦法做到這一點?

已編輯

我使用隱藏輸入和這樣的 js 函數做了一個解決方法

//on the liquid file i added the hidden field with a custom class with the sub-total of that row
{% for item in cart.items %}
    {% item.quantity >= 6 %}
        <input type="hidden" value="{{ price | times: item.quantity }}" class="subtotals">
    {% else %}
        <input type="hidden" value="{{ sca_price }}" class="subtotals">
    {% endif %}
{% endfor %}
//in the function i calculate de sum of all the rows and show the result on a div called show_subtotal
function calculateSubtotal()
{   
    var subtotal = parseInt(0);
    $('.subtotals').each(function(index, el) {
        subtotal += parseInt($(el).val());
    });
    subtotal = '$'+subtotal;
    subtotal = subtotal.slice(0,-2)+'.'+subtotal.slice(-2)
    $('.show_subtotal').html(subtotal);
}

這項工作,但這不是最好的方法,我應該能夠在服務器端做到這一點。 有什么建議嗎?

嗯,再用一次assign

{% assign subtotal = 0 &}
{% item.quantity >= 6 %}
    //if the item qty is greater than 6 i show a different prices, in this case a price saved on a metafield
    {% assign subtotal = item.product.metafields.global.wholesale_price | times: item.quantity %}
{% else %}
    {% assign subtotal =  item.price | times: item.quantity %}
{% endif %}

你可以試試這個:

{{ item.final_line_price | money }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM