Percent of a subset (by vendor list), with min/max caps

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

{% liquid
  # Shipping is a percentage of ONLY items from a chosen vendor list.
  # Example: charge 8% of all ACME/Contoso items, capped between $3–$20.

  # --- editable settings ---
  assign service_name = "Vendor Subset Shipping"
  assign service_code = "VENDOR_SUBSET"
  assign currency = "USD"
  assign description = "Calculated on selected vendors only"
  assign shipping_percentage = 8                # percent of subset subtotal
  assign vendors_raw = "ACME|Contoso"           # pipe-separated list, exact vendor names
  assign min_price = 300                        # cents
  assign max_price = 2000                       # cents

  # --- logic ---
  assign vendors = vendors_raw | split: "|"     # array of vendor names
  assign subset_total = 0

  for item in shopify_rate_check.items
    if vendors contains item.vendor
      assign line_price = item.price | times: item.quantity
      assign subset_total = subset_total | plus: line_price
    endif
  endfor

  assign shipping_price = subset_total | times: shipping_percentage | divided_by: 100
  assign shipping_price = shipping_price | at_least: min_price | at_most: max_price

  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