Solved

What is the process for setting shipping methods and obtaining product costs in Shopify liquid?

PoolsAndMore
Visitor
2 0 0

Hi All,

I am pretty new to shopify and liquid. I am trying to build some xml using some liquid variables and have it all except the shipping method and the product cost.

My shipping method is based on total weight in pounds. This is what i attempted but does not seem to work as it is always setting to FEDEX

{% assign: total_weight = 0.000 %}
{% for line in line_items %}
{% assign total_weight = total_weight | plus: line.quantity | times: line.grams %} {% endfor %}
{% assign total_weight = total_weight | times: .00220462262185%}
//I can see here that the weight seems to calculate correct.  I think my case statement maybe isnt right
{% case {total_weight} %}
{% when total_weight <= .98 %}
{% assign : shipping_method = 'USPS-First Class' %}
{% when {total_weight} <= 2 %}
{% assign : shipping_method = ' USPS-priority' %}

{% else %}
{% assign : shipping_method = 'FEDEX' %}

{% endcase %}
<ship_method>{{shipping_method}}</ship_method>

 

My other issue is how can I get product costs? My distributor requires for each order I send the cost I pay. I have this in my shopify products but can't find where to get it from liquid.

Thanks for any assistance

Accepted Solution (1)

tim
Shopify Expert
3258 232 1178

This is an accepted solution.

There are couple of things I do not like in your code.

  1. Wrong order when doing math  -- you're adding first and then multiplying the sum;
  2. case/when can only match values

I'd do it like:

{% assign: total_weight = 0.000 %}
{% for line in line_items %}
	{% assign line_weight = line.quantity | times: line.grams %}
	{% assign total_weight = total_weight | plus: line_weight %} 
{% endfor %}
{% assign total_weight = total_weight | times: .00220462262185%}

{% if total_weight <= .98 %}
	{% assign shipping_method = 'USPS-First Class' %}
{% elsif total_weight <= 2 %}
	{% assign shipping_method = ' USPS-priority' %}
{% else %}
	{% assign shipping_method = 'FEDEX' %}
{% endif %}

<div class=ship_method>{{shipping_method}}</div>

 

View solution in original post

Replies 2 (2)

tim
Shopify Expert
3258 232 1178

This is an accepted solution.

There are couple of things I do not like in your code.

  1. Wrong order when doing math  -- you're adding first and then multiplying the sum;
  2. case/when can only match values

I'd do it like:

{% assign: total_weight = 0.000 %}
{% for line in line_items %}
	{% assign line_weight = line.quantity | times: line.grams %}
	{% assign total_weight = total_weight | plus: line_weight %} 
{% endfor %}
{% assign total_weight = total_weight | times: .00220462262185%}

{% if total_weight <= .98 %}
	{% assign shipping_method = 'USPS-First Class' %}
{% elsif total_weight <= 2 %}
	{% assign shipping_method = ' USPS-priority' %}
{% else %}
	{% assign shipping_method = 'FEDEX' %}
{% endif %}

<div class=ship_method>{{shipping_method}}</div>

 

tim
Shopify Expert
3258 232 1178

Also, AFAIK variant cost field is not available in liquid.