Shopify offers a relatively nice way to handle automatic discount in the Checkout based on certain criteria - for example, if the customer has added X product to the cart, automatically apply Y discount.
A common use-case for these automatic discounts is often the ability to offer employee or 'Friends & Family' discount. The idea is that if a specific customer is logged in and checking out, we want to apply a discount automatically.
The native solution that Shopify offers is great - but it doesn't allow you much flexibility beyond a global setting. What happens if you'd like to enable an automatic employee discount, except for products in a specific collection?
This is exactly what I needed to do. I wanted to be able to apply an automatic discount to employee customers but exclude certain Collections.
Enter Script Editor - a useful app from Shopify that enables developers to securely execute Ruby scripts in the checkout.
There are definitely apps that can do this, but I don't think that it's entirely necessary and potentially just adds a tonne of bloat to the shop.
You'll first need to ensure that your customers are assigned a specific tag so that the script can identify them, the one I've gone for is is-employee
. You can add these tags manually to the customer from the admin panel or use an automatic solution like Shopify Flow.
Next, we'll want to locate and set the products in which we'd like to exclude from discount. Unfortunately, Shopify Scripts doesn't have any awareness of the Collection
object - so this makes adding bulk product exclusions a bit trickier. Instead, you'll have to us product tags, sa Scripts has visiblity of product.tags
. So you'll need to tag any product that you'd like to be excluded with a specific tag. In my case, I chose ed-exclude
.
Below is the script that I came up with, and whilst I'm not a Ruby programmer, I don't think it's too bad:
##################################
### LINE ITEM SCRIPTS
### - Discounts Friends and Family
##################################
##################################
### VARIABLES
##################################
CUSTOMER_IS_EMPLOYEE = false
DISCOUNT_MESSAGE = "Employee 30% Discount"
DISCOUNT_PERCENTAGE = 30
TAG_EXCLUDE_PRODUCT = "ed-exclude"
TAG_EMPLOYEE = "is-employee"
customer = Input.cart.customer
##################################
### EMPLOYEE DISCOUNT
### If the current customer is tagged
### as an employee, apply a discount
### to items that aren't tagged
### to be excluded from discounts.
##################################
if customer
if customer.tags.include?(TAG_EMPLOYEE) # Check if the customer is logged in and is tagged with our target tag
CUSTOMER_IS_FF = true
end
end
if CUSTOMER_IS_EMPLOYEE
Input.cart.line_items.each do |line_item| # Loop over each cart item
product = line_item.variant.product # Access the actual product object
next if product.tags.include? TAG_EXCLUDE_PRODUCT # If the product contains our excluded tag, skip the current line item.
DISCOUNT = ((100 - DISCOUNT_PERCENTAGE) / 100)
line_item.change_line_price(line_item.line_price * DISCOUNT, message: DISCOUNT_MESSAGE) #Apply the overall discount to the line-item.
end
end
##################################
### OUTPUT
##################################
Output.cart = Input.cart
In this script, I am:
- Checking if the current cutomer is tagged with the employee tag
- If yes, loop over products in the cart
- If any of the products contain our 'excluded' tag, skip it
- If the product is discountable, modify the line item price to the discounted amount.
This could definitely be modified to only include products that contain a specific tag, but I felt that this was a nice solution to a common problem that I see around Shopify, and I'm glad that I didn't need to install an app to solve this problem.