Shopify Script Editor – Variable Employee Discount

Employee discounts are a hassle in shopify. In shopify plus you’re given a script editor to do some of the work so you don’t have to rely on an app.

In my case i needed to be able to give certain items different employee discounts. You would think that meta fields would help with this. However the Script Editor does not have access to the meta fields object.

I decided to use tags and split on a colon. “discount : INT”

Bare in mind i’m not a ruby programmer.

if product.tags.length != 0
  product.tags.each do |tag|
  if tag.include?"discount"

    # grab the string "discount : 20"
    # split string on :, strip spaces, set to flow and divide by 100 to get the decimal we need.
    variable_discount = (tag.split(':')[1].strip.to_f)/100
    ff_discount = 0.2

    if employee == 'company-name'
      #  Employees get a varying discount
      discount = variable_discount
      message = "Employee: #{discount*100}% Discount"
    else
      # Everyone else gets a friends and fam 20% or discount less than 20%
      discount = variable_discount < ff_discount ? variable_discount : ff_discount
      message = "Friends & Family: #{discount*100}% Discount"
    end
    line_item.change_line_price(line_item.line_price*(1.0-discount), message: message)
  end
end

I use shopify’s flow to assign a customer tag of “employee : company-name” at user sign up. With this method you can have different tiers of employee discounts.

We have an employee discount and a friends and family discount. for now we are doing “employee : friends” to flag this. Eventually this will get more complex.

variable_discount = (tag.split(':')[1].strip.to_f)/100

This line is where the magic happens.

  • grab the ‘discount : 50’ tag as a string
  • split the string on the :
  • strip the white space
  • string to float type conversion
  • divide by 100 to make it fit with the line_item change price code

If you have any questions. please hit me up on twitter @nerdwithus