I feel so close with this one, and maybe someone can point out my mistake really easily. I am trying to write a for/loop statement that will display the estimated shipping price on the cart page before the customer goes to check out. These are just example numbers, but the problem I am running into is that the “Then” statements are only applying to each item once. For example, with this code:
{% assign shipping_price = 0 %}
{% for item in cart.items %}
{% case item.product.type %}
{% when 'DECORATIVE OBJECTS' %}
{%- assign shipping_price = shipping_price | plus: 100 -%}
{% when 'NIGHTSTANDS' %}
{%- assign shipping_price = shipping_price | plus: 200 -%}
{% when 'CONSOLE TABLES' %}
{%- assign shipping_price = shipping_price | plus: 300 -%}
{% endcase %}
{% endfor %}
{% echo shipping_price | money_with_currency %}
if I add two different products that are both of type “DECORATIVE OBJECTS” cart will correctly show shipping as $2.00, but then when I change the quantity of them, the shipping price still only says $2.00, but I want it to keep adding one dollar for each count of the product.
I tried adding “| times: item.quantity”
{% assign shipping_price = 0 %}
{% for item in cart.items %}
{% case item.product.type %}
{% when 'DECORATIVE OBJECTS' %}
{%- assign shipping_price = shipping_price | plus: 100 | times: item.quantity -%}
{% when 'NIGHTSTANDS' %}
{%- assign shipping_price = shipping_price | plus: 200 | times: item.quantity -%}
{% when 'CONSOLE TABLES' %}
{%- assign shipping_price = shipping_price | plus: 300 | times: item.quantity -%}
{% endcase %}
{% endfor %}
{% echo shipping_price | money_with_currency %}
which almost works, but then the math starts to get weird when you have multiple products in your cart. I can’t really figure out how it is calculating, but its not what we need.
I thought maybe I needed a nested for/loop to loop through each product that meets the condition and then applies the “Then” statement, but I can’t quite figure out how to apply that nested for/loop. If someone thinks that could work and knows how to do it, I would be so grateful to see how to do that here.
Any help at all will be so appreciated.
Thanks!