Overview
This document was prepared to help illustrate how our merchants can manage a Spend & Save Tiered discount model on subscription orders.
This example has both AUD and NZD tiers in the code; they offer a base discount of 5% off subscription, and then the discount increases based on the spend of the total order.
The code dump of the Run code action is below, but there are also additional steps in the Flow to handle errors.
Example tiers
Subscribe & Save 5%
Subscribe & Save 5% + 5%
Subscribe & Save 5% + 25%
Subscribe & Save 5% + 30%
Run code excerpt
javascriptconst TIERS = { AUD: [ { threshold: 0, discount: "Subscribe & Save 5%", discountAliases: [ "Submarine - Subscribe & Save 5%" ] }, { threshold: 13000, discount: "Subscribe, Spend & Save 5% + 5%", discountAliases: [ "Submarine - Subscribe, Spend & Save 5% + 5%" ] }, { threshold: 17000, discount: "Subscribe, Spend & Save 5% + 25%", discountAliases: [ "Submarine - Subscribe, Spend & Save 5% + 25%" ] }, { threshold: 26300, discount: "Subscribe, Spend & Save 5% + 30%", discountAliases: [ "Submarine - Subscribe, Spend & Save 5% + 30%" ] }, ], NZD: [ { threshold: 0, discount: "Subscribe & Save 5%", discountAliases: [ "Submarine - Subscribe & Save 5%" ] }, { threshold: 14500, discount: "Subscribe, Spend & Save 5% + 5%", discountAliases: [ "Submarine - Subscribe, Spend & Save 5% + 5%" ] }, { threshold: 19900, discount: "Subscribe, Spend & Save 5% + 25%", discountAliases: [ "Submarine - Subscribe, Spend & Save 5% + 25%" ] }, { threshold: 29900, discount: "Subscribe, Spend & Save 5% + 30%", discountAliases: [ "Submarine - Subscribe, Spend & Save 5% + 30%" ] }, ], }; const INELIGIBLE_AUTOMATIC_DISCOUNTS = ["Subscribe & save extra 5%"]; export default function main(input) { let subtotalCurrency = "AUD"; let subtotalAmountCents = 0; // calculate the new subscription subtotal input.subscription.lines.forEach((line) => { subtotalCurrency = line.basePrice.currency; const lineAmountCents = parseInt(parseFloat(line.basePrice.amount) * 100) * line.quantity; subtotalAmountCents += lineAmountCents; }); // calculate the tiers this subscription is eligible and ineligible for let currencyTiers = TIERS[subtotalCurrency]; let eligibleTierDiscount = currencyTiers[0].discount; // calculate the maximum tier discount this subscription is eligible for currencyTiers.forEach((tier) => { if (tier.threshold <= subtotalAmountCents) { eligibleTierDiscount = tier.discount; } }); // build a list of all the other tier discounts that we may need to remove const ineligibleTierDiscounts = currencyTiers .filter((tier) => tier.discount !== eligibleTierDiscount) .flatMap((ineligibleTier) => [ineligibleTier.discount, ...ineligibleTier.discountAliases]) // format subtotal amount const subtotalAmount = (subtotalAmountCents / 100.0).toFixed(2); const ineligibleAutomaticDiscounts = INELIGIBLE_AUTOMATIC_DISCOUNTS; return { eligibleTierDiscount, ineligibleAutomaticDiscounts, ineligibleTierDiscounts, subtotalCurrency, subtotalAmount, subtotalAmountCents, }; }
Notes
Adding Conditions.
To reduce noise, you could add a condition between the Subscription update and Run code steps to check if the Lines were edited; it is the only way to change the tiered spend.
- You can also use this same Flow but replace the Subscription updated trigger for the Upcoming order notification trigger - however for testing purposes itβs much easier to use the Updated trigger.
Importing Sample Flow
Below is the file you can import to Shopify Flow.
To get this to work, you will need to set the submarineApiKey secret in Flow.
- To do this, click Add secret below the field
- Open a new tab with Submarine App
- Open Submarine Settings
- Navigate to GraphQL API
- Copy the API key and paste it into the Secrets form field Secret value
- Click Save
The next step to get these to run is to ensure you have matching discount codes for Amount off order in Shopify Discounts
Create discount codes
Example: Create an amount-off order discount code with the name
Subscribe, Spend & Save 5% + 30%The discount code setup should have the following settings.
Run Flow
Once you have added the API Secret and created any discount codes that you want to use you can make changes to a test Subscription and check how the Flow runs to add or remove a discount code.