How to create a script for automatic staff discounts?

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 :slightly_smiling_face:

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. :thinking: 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

@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

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

Thanks,
Pooja