skip to Main Content

I am showing different prices on the cart based on the quantity of an item, if the item in the cart has more than 6 in the item.quantity it should count the price saved in a metafield, else it shown the default price. knowing this i want to show the total on the cart based on those prices, for example

//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 %}

and finally show the subtotal

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

as i have seen doing subtotal += value is not supported or i am using it wrong, is there a way to do this?

Edited

i did a workaround using hidden inputs and a js function like this

//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);
}

this work but it is not the best way to do it, i should be able to do that on server side. any advice?

2

Answers


  1. Hm, just use assign again

    {% 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 %}
    
    Login or Signup to reply.
  2. You can try this:

    {{ item.final_line_price | money }}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search