How to create a script for automatic staff discounts?

Solved

How to create a script for automatic staff discounts?

PRam
New Member
6 0 0

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 🙂

Accepted Solutions (2)

Kani
Shopify Partner
468 125 227

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

 

 

 

 

 

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂

View solution in original post

Kani
Shopify Partner
468 125 227

This is an accepted solution.

@PRam 

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
Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂

View solution in original post

Replies 6 (6)

Kani
Shopify Partner
468 125 227

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

 

 

 

 

 

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂
PRam
New Member
6 0 0

@Kani  Is there a way where I can disable the use of discount codes for staff that are receiving this discount please?

Regards,
Pooja

Kani
Shopify Partner
468 125 227

sorry I'm not sure what you want. 🤔 if you don't mind, can you explain in more detail?

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂
PRam
New Member
6 0 0

@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


Kani
Shopify Partner
468 125 227

This is an accepted solution.

@PRam 

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
Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂
PRam
New Member
6 0 0

@Kani am receiving some issues from the script, I have sent you a message 
Can you please have a look ?

Thanks,
Pooja