Personalized checkout and custom promotions with Shopify Scripts
Hello,
I needed some help to create a script for staff discounts.
Anyone's email containing our company domain will automatically get a discount of 40%
For example:
(Any)@companydomain.com
*#AZ-1-9#*@companydomain.com
while checking out they will get the 40% off.
I came across this script, but wondering how to check the email format: https://ashcroft.dev/blog/shopify-script-add-employee-product-discount/
Thanks 🙂
Solved! Go to the solution
This is an accepted solution.
Hi @PRam
you can simply do it like this
##################################
### VARIABLES
##################################
CUSTOMER_IS_EMPLOYEE = false
DISCOUNT_MESSAGE = "Employee 30% Discount"
DISCOUNT_PERCENTAGE = 30
TAG_EXCLUDE_PRODUCT = "ed-exclude"
TARGET_DOMAIN = 'companydomain.com'
customer = Input.cart.customer
##################################
### EMPLOYEE DISCOUNT
### If the current customer email contains target domain
### as an employee, apply a discount
### to items that aren't tagged
### to be excluded from discounts.
##################################
# customer is not empty and email contains target domain
if customer and customer.email.include?(TARGET_DOMAIN)
CUSTOMER_IS_EMPLOYEE = true
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
This is an accepted solution.
please check it
##################################
### VARIABLES
##################################
CUSTOMER_IS_EMPLOYEE = false
DISCOUNT_MESSAGE = "Employee 30% Discount"
DISCOUNT_PERCENTAGE = 30
TAG_EXCLUDE_PRODUCT = "ed-exclude"
TARGET_DOMAIN = 'companydomain.com'
customer = Input.cart.customer
##################################
### EMPLOYEE DISCOUNT
### If the current customer email contains target domain
### as an employee, apply a discount
### to items that aren't tagged
### to be excluded from discounts.
##################################
# customer is not empty and email contains target domain
if customer and customer.email.include?(TARGET_DOMAIN)
CUSTOMER_IS_EMPLOYEE = true
end
# is employee and has not use discount code
if CUSTOMER_IS_EMPLOYEE and Input.cart.discount_code == nil
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
This is an accepted solution.
Hi @PRam
you can simply do it like this
##################################
### VARIABLES
##################################
CUSTOMER_IS_EMPLOYEE = false
DISCOUNT_MESSAGE = "Employee 30% Discount"
DISCOUNT_PERCENTAGE = 30
TAG_EXCLUDE_PRODUCT = "ed-exclude"
TARGET_DOMAIN = 'companydomain.com'
customer = Input.cart.customer
##################################
### EMPLOYEE DISCOUNT
### If the current customer email contains target domain
### as an employee, apply a discount
### to items that aren't tagged
### to be excluded from discounts.
##################################
# customer is not empty and email contains target domain
if customer and customer.email.include?(TARGET_DOMAIN)
CUSTOMER_IS_EMPLOYEE = true
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
@Kani Is there a way where I can disable the use of discount codes for staff that are receiving this discount please?
Regards,
Pooja
sorry I'm not sure what you want. 🤔 if you don't mind, can you explain in more detail?
@Kani My apologies,
If am an employee receiving this discount at checkout, I should not be able to use additional Discount codes for this purchase
This is an accepted solution.
please check it
##################################
### VARIABLES
##################################
CUSTOMER_IS_EMPLOYEE = false
DISCOUNT_MESSAGE = "Employee 30% Discount"
DISCOUNT_PERCENTAGE = 30
TAG_EXCLUDE_PRODUCT = "ed-exclude"
TARGET_DOMAIN = 'companydomain.com'
customer = Input.cart.customer
##################################
### EMPLOYEE DISCOUNT
### If the current customer email contains target domain
### as an employee, apply a discount
### to items that aren't tagged
### to be excluded from discounts.
##################################
# customer is not empty and email contains target domain
if customer and customer.email.include?(TARGET_DOMAIN)
CUSTOMER_IS_EMPLOYEE = true
end
# is employee and has not use discount code
if CUSTOMER_IS_EMPLOYEE and Input.cart.discount_code == nil
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
@Kani am receiving some issues from the script, I have sent you a message
Can you please have a look ?
Thanks,
Pooja
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024