Skip to main content
All CollectionsIntegrationsOMS Integrations - Setting up your OMS
How to offer free shipping with discount code?
How to offer free shipping with discount code?

Adding this script will discount the shipping cost when a discount code is used at the checkout.

Vineeta Pendse avatar
Written by Vineeta Pendse
Updated over a week ago

When you select to send store credit from yayloh, we generate & send a discount code from your Shopify store. This discount code does not apply to the shipping cost at the checkout by default.

If you wish to offer free shipping at check out for your customer, Shopify allows you to add a small script/code to your store's back office. You can choose only one of two scripts below to add to your Shopify store based on your preference:

You can follow the instructions given below based on the script you choose.


Option 1: Discount shipping cost

Adding this script will discount the shipping cost when a discount code with the prefix of "YAY" is used at check out.

Add the Script to your Shopify:

  1. Under apps in Shopify, download and install 'Script Editor'.

  2. Open ‘Script Editor’ from the side menu bar of your Shopify store.

  3. Click on “Create script” button.

  4. Select “Blank template” and click on “Create script”.

  5. Title your script as 'yayloh checkout script to discount shipping cost'.

  6. Open the code layout for the script and paste the code given below.

    discount = if Input.cart.discount_code.code.start_with?("YAY")
    1
    else
    0
    end
    message = "Free shipping with your discount code"

    Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.source == "Shopify"
    shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
    end

    Output.shipping_rates = Input.shipping_rates

  7. The code layout should look the same as the script depicted below.

  8. Save your script as a draft.


Test the script:

  1. Switch to the input tab.

  2. Under the products, search & select a product to add to the test cart. Make sure the product is in stock.

  3. Under customer, search & select any customer email.

  4. Under discount codes field, search for a discount code created by yayloh by typing 'yay' and selecting any old discount code.

    Note- If necessary create a new discount code for 'Amount off order' starting with 'YAY' for the selected customer email ID. (Learn how). You can always delete this newly created code after testing.

  5. Click on Run the script at the bottom of the input window.

  6. Verify the result.

Publish the script

  1. Save and publish the script.


Option 2: Discount shipping cost only if order value is 0.

Adding this script will discount the shipping cost when a discount code with the prefix of "YAY" is used at check out and the order value is 0.

Add the Script to your Shopify:

  1. Under apps in Shopify, download and install 'Script Editor'.

  2. Open ‘Script Editor’ from the menu bar.

  3. Click on create script button.

  4. Select “Blank template” and click on “Create script”.

  5. Title your script as 'yayloh checkout script to discount shipping rate'

  6. Open the code layout for the script and paste the code given below.

    discount_amount = Money.new(cents:100) * 0
    total = Input.cart.subtotal_price_was
    discount = if total = discount_amount
    1
    else
    0
    end
    message = "Free shipping if order is equal to 0"
    Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.source == "shopify"
    shipping_rate.apply_discount(shipping_rate.price * discount, message: message)
    end
    Output.shipping_rates = Input.shipping_rates

  7. The code layout should look the same as the script depicted below.

  8. Save your script as a draft.


Test the script:

  1. Switch to the input tab.

  2. Under the products field, search & select a product to add to the test cart. Make sure the product is in stock.

  3. Under customer field, search & select any customer email.

    1. Under discount codes field, search for a discount code created by yayloh by typing 'yay' and selecting any old discount code.

      Note- If necessary create a new discount code for 'Amount off order' starting with 'YAY' for the selected customer email ID. (Learn how). You can always delete this newly created code after testing.

      1. Click on “Run script" at the bottom of the input window.

      2. Verify the result.

Publish the script

  1. Save and publish the script.


    Important Note-

    These scripts are written for discount codes starting with "YAY". As this is the prefix for all discount codes generated by yayloh. If your team creates any discount codes with this prefix while one of these scripts is published, the rule will apply to them too.

Did this answer your question?