Skip to main content

Updating the Wholesale Discount Script

  1. Login to Shopify Admin

  2. Click on APPS

  3. Click on the Shopify Script App

  4. On the main screen you will see two items -

    1. Wholesale - TGTG Discount Script
      Allows discount stacking
    2. Wholesale - TGTG Discount Script - No Stack (PUBLISHED)
      Does not allow stacking
  5. You CANNOT edit live/published scripts, but you can duplicate them to work on them.

  6. Click on Wholesale - TGTG Discount Script - No Stack

  7. On the top of the page, click the DUPLICATE button

  8. Once duplicated, you can change the title to something more descriptive

  9. Interface below:
    Left Side = Source code of the script
    Center = Cart where you can test different options to determine if the updates work as intended
    Right = The output screen, click on RUN SCRIPT to see the generated output from your cart.

    Note, if you don’t see all three screens, click on the CODE / INPUT / BOTH buttons to toggle around.

  1. Editing the code blocks

Set the variable TAGPRICING = the customer tag you want this script to target.

TAGPRICING = 'Wholesale - TGTG'

Leave CUST_DISCOUNT alone

CUST_DISCOUNT = false

Customer Check Block
This block checks to see if the customer on the checkout is tagged with TAGPRICINGs variable. If so, we set the CUST_DISCOUNT = true

if !Input.cart.customer.nil?
Input.cart.customer.tags.each do |tag|
if tag == TAGPRICING
CUST_DISCOUNT = true
end
end
end

Discount Code Reject Script Discount

I wrote this out fully so it is simpler to read. The first if checks if the user already is able to get the discount, if so - the next if checks if the discount code parameter exists on the checkout. If there is a discount code (the else block) we set the CUST_DISCOUNT variable to false, as they no longer can qualify for the script discount.

if CUST_DISCOUNT
if Input.cart.discount_code.nil?

else
CUST_DISCOUNT = false
end
end

Actual Discounting (wrapping added for easier reading below)

If the user has qualified for the discount, we loop through each line item and change the price. Multiplying by 0.95 will give you a 5% discount on each line item. MESSAGE = the message displayed to the customer.

if CUST_DISCOUNT
Input.cart.line_items.each do |line_item|
line_item.change_line_price((line_item.line_price * 0.95),
message: "5% Discount")
end
end\

Some further info on shopify scripts:

https://www.shopify.com/partners/blog/109804486-getting-started-with-shopify-scripts-a-practical-walkthrough

Online Script Builder - This is very much a use at your own risk as it typically outputs really complex code for simple items that is hard to read and should always be thoroughly checked

https://jgodson.github.io/shopify-script-creator/