Customizing the Initial Offer Incentive (IOI) using Shopify Scripts

If you are trying to offer your customers an Initial Offer Incentive, you may be able to use our feature in your Ordergroove Admin. However, if you want to customize your customer experience even further, recommend you use a Shopify Script.
Shopify Scripts are small pieces of code that let you create unique experiences for your customers in their cart and at checkout. Find out more here on Shopify Help Center articles.

Caution: Some aspects of this article may require technical expertise with coding languages.

If you do not currently have someone on staff or contract that can provide that level of technical assistance, Ordergroove suggests seeking out that level of support before attempting to add or alter any working code relating to your subscriptions or storefront.


Getting Started

Our script below is a starting point that allows you to give customers a percent or dollar amount off their subscription purchase. To get started, install the Shopify Script Editor app if you don't already have it.
Create a new script for "Line Items" and begin with a blank template:



Once you have that, you can add the following code. This script will give a customer a discount on the subscription item only for whatever percentage you set. You can change the discount_amount and the discount_message to whatever you'd like. If you'd like to change this further, we recommend using Shopify's excellent documentation on their Script Editor.
DISCOUNTS = [
{
  discount_type: :percent,
  discount_amount: 25,
  discount_message: '25% off subscription products!'
}
]
class DiscountApplicator

def initialize(discount_type, discount_amount, discount_message)
  @discount_type = discount_type
  @discount_message = discount_message
  @discount_amount = if discount_type == :percent
    1 - (discount_amount * 0.01)
  else
    Money.new(cents: 100) * discount_amount
  end
end

def apply(line_item)
  new_line_price = if @discount_type == :percent
    line_item.line_price * @discount_amount
  else
    [line_item.line_price - (@discount_amount * line_item.quantity), Money.zero].max
  end
  line_item.change_line_price(new_line_price, message: @discount_message)
end
end

class OrdergrooveDiscountCampaign
def initialize(discounts)
  @discounts = discounts
end

def subscription(line_item)
  !line_item.selling_plan_id.nil?
end

def run(cart)
  @discounts.each do |discount|
    discount_applicator = DiscountApplicator.new(
      discount[:discount_type],
      discount[:discount_amount],
      discount[:discount_message]
    )
    cart.line_items.each do |line_item|
      if subscription(line_item)
          discount_applicator.apply(line_item)
      end
    end
  end
end
end
CAMPAIGNS = [
OrdergrooveDiscountCampaign.new(DISCOUNTS),
]
CAMPAIGNS.each do |campaign|
campaign.run(Input.cart)
end
Output.cart = Input.cart

Once you copied the code in, click on the Save and Publish button to complete it.

Further Customizations

For further customizations to the above script, you can reference Shopify's excellent documentation on the Shopify Help Center. You'll find great examples of popular scripts and more that you can adapt and use with your subscription program.

Comments

0 comments

Article is closed for comments.