First item + each additional (physical items only), with free-over threshold

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

{% liquid
  # Charge $5 for the first shippable item, $2 for each additional shippable item.
  # If cart subtotal >= $100, shipping is free.

  # --- editable settings ---
  assign service_name = "Incremental Shipping"
  assign service_code = "INCREMENTAL"
  assign currency = "USD"
  assign description = "First item + additional items"
  assign first_item_price = 500                 # cents
  assign additional_item_price = 200            # cents per extra item
  assign free_over_subtotal = 10000             # cents threshold for free shipping

  # --- logic ---
  assign subtotal = 0
  assign shippable_qty = 0

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

    if item.requires_shipping
      assign shippable_qty = shippable_qty | plus: item.quantity
    endif
  endfor

  if subtotal >= free_over_subtotal
    assign shipping_price = 0
  else
    assign extra_qty = shippable_qty | minus: 1
    if extra_qty < 0
      assign extra_qty = 0
    endif
    assign shipping_price = first_item_price | plus: (additional_item_price | times: extra_qty)
  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