Price + Weight blended logic (free over $X, heavy surcharge over Y grams)

Created by Lisa Erkens, Modified on Mon, 13 Oct at 4:49 PM by Lisa Erkens

{% liquid
  # Compose price and weight rules:
  # - Free if subtotal >= $75 (US only)
  # - Else base price
  # - If total weight > 5 kg, add $6 heavy surcharge

  # --- editable settings ---
  assign service_name = "Economy"
  assign service_code = "ECONOMY"
  assign currency = "USD"
  assign description = "Blended price & weight rules"
  assign base_price = 899                       # cents
  assign heavy_threshold_grams = 5000           # 5 kg
  assign heavy_surcharge = 600                  # cents
  assign free_over_cents = 7500                 # $75
  assign free_country = "US"                    # only free for this country

  # --- logic ---
  assign subtotal = 0
  assign total_grams = 0

  for item in shopify_rate_check.items
    assign line_price = item.price | times: item.quantity
    assign subtotal = subtotal | plus: line_price
    assign line_weight = item.grams | times: item.quantity
    assign total_grams = total_grams | plus: line_weight
  endfor

  assign shipping_price = base_price

  if shopify_rate_check.destination.country == free_country and subtotal >= free_over_cents
    assign shipping_price = 0
  endif

  if shipping_price > 0 and total_grams > heavy_threshold_grams
    assign shipping_price = shipping_price | plus: heavy_surcharge
  endif

  assign rate = rate | merge: "currency", currency
  assign rate = rate | merge: "service_name", service_name
  assign rate = rate | merge: "service_code", service_code
  assign rate = rate | merge: "total_price", shipping_price
  assign rate = rate | merge: "description", description

  assign rates = rates | array_append: rate
%}
{
  "rates": {{ rates | array_wrap | json }}
}

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article