Remove Discount Codes from Prepaid Subscriptions During Checkout

Prepaid subscriptions usually offer a steep discount upfront to entice subscribers to commit to a longer timeframe. This can cause complications if a customer tries to add a discount code during checkout, it could stack with the built-in discount, creating a much larger discount than intended.

If you are on Shopify Plus, you can use Shopify Scripts to remove discount codes on initial orders that have a prepaid item in it, limiting customers to the intended subscription program discount.

Note: This article includes sample code for a script to remove a discount code from prepaid orders. Ordergroove is able to provide the sample code, however, we cannot offer support on customizing the script so that it works for your store if there is any specific case you want to cover for.


What will the script do?

The script will try to identify any subscription that has a Variant Price higher than the Compare_at_price of the variant. Prepaid subscriptions will always have a price higher than the Compare_at_price as they can be 3-12 times more expensive to account for future orders.

As a safety measure, we multiply the compare_at_price 1.5 times to reduce the risk that a standard pay-as-you-go subscription has a higher variant price than the compare_at_price. Prepaid variant prices can be calculated this way:

Prepaid price = (number of shipments1 * variant_price ) (1 - prepaid_discount_percentage)

1number of shipments being between a 2 and a 12.

Warning: Shopify plans to deprecate Shopify Scripts on August 13th, 2023. Ordergroove will provide a more permanent solution with the use of Shopify Functions, the process outlined in this document should be considered a temporary solution.


Getting started

Step 1 - Install the Script Editor

We're going to use Shopify's script editor for this process. Install Script Editor in your store if you haven't already, you can use this direct link to find the listing.

Step 2 - Create a Blank Script

Create a new blank script. Only one script per script type can be published at a time.

image3.png

image5.png

Step 3 - Add Code to Your Script

Add the following code to the script. Feel to change the error_message, this will display in checkout to your customers. 

discount_removal_error_message 'Discount cannot be applied to a Prepaid Subscription. Remove it if you want the discount to apply to other items'

should_remove_discount_from_prepaid = false

if Input.cart.discount_code
  Input.cart.line_items.each do |line_item|
    if line_item.selling_plan_id != nil and line_item.variant and line_item.variant.compare_at_price and line_item.variant.price > (line_item.variant.compare_at_price * 1.5)
      should_remove_discount_from_prepaid = true
      break
    end
  end
end

if should_remove_discount_from_prepaid
  Input.cart.discount_code.reject({ message: discount_removal_error_message })
end

Output.cart = Input.cart

Step 4 - Save and Test it Out

Save and publish the script. If you want to change it later, you will need to unpublish the script and publish it again.

Once you're finished, add a prepaid item to your cart and try to add a discount code during checkout. This should be the result:

image4.png

Whenever the customer adds a prepaid subscription to the cart, no discount code will work for that order. If there is no prepaid subscription on the order, it will work normally.

Keep in mind that the script editor only allows removing the discount code from the entire order, not from specific items. If the customer wants to use a discount code on the non-prepaid items in their order, they will need to check out with those items separately.


Further Customizations

Take a look at Shopify's documentation in their Help Center for additional information and examples of popular scripts that you can adapt and use with your subscription program.