All things Shopify and commerce
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
Hi,
I am currently using shopify script to create a line item script to provide discount to employee based on customer tag and product tag. How can I create script using shopify function.
Here is my code :
REJECTION_MESSAGE = 'Discount codes cannot be used during this sale'
CUSTOMER_IS_EMPLOYEE = false
DISCOUNT_MESSAGE_30 = "Employee 30% Discount"
DISCOUNT_PERCENTAGE_30 = 30
TAG_EMP_30 = "EMP_30"
DISCOUNT_MESSAGE_40 = "Employee 40% Discount"
DISCOUNT_PERCENTAGE_40 = 40
TAG_EMP_40 = "EMP_40"
DISCOUNT_MESSAGE_50 = "Employee 50% Discount"
DISCOUNT_PERCENTAGE_50 = 50
TAG_EMP_50 = "EMP_50"
DISCOUNT_MESSAGE_75 = "Employee 75% Discount"
DISCOUNT_PERCENTAGE_75 = 75
TAG_EMP_75 = "EMP_75"
TAG_EMPLOYEE = "Employee"
customer = Input.cart.customer
######################################
CART_REJECTION_MESSAGE ="discount code can not be applied"
condition_is_met = false
is_discount_applied = false
##########################################
# employee discount
if customer
if customer.tags.include?(TAG_EMPLOYEE) and customer.email.end_with?("@abc.com") || customer.email.end_with?("@xyz.com") # Check if the customer is logged in and is tagged with our target tag
CUSTOMER_IS_EMPLOYEE = 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
# EMP_75 tag
if product.tags.include?(TAG_EMP_75)
DISCOUNT = ((100 - DISCOUNT_PERCENTAGE_75) / 100)
line_item.change_line_price(line_item.line_price * DISCOUNT, message: DISCOUNT_MESSAGE_75) #Apply the overall discount to the line-item.
if Input.cart.discount_code != nil
is_discount_applied = true
end
end
# EMP_50 tag
if product.tags.include?(TAG_EMP_50)
DISCOUNT = ((100 - DISCOUNT_PERCENTAGE_50) / 100)
line_item.change_line_price(line_item.line_price * DISCOUNT, message: DISCOUNT_MESSAGE_50) #Apply the overall discount to the line-item.
if Input.cart.discount_code != nil
is_discount_applied = true
end
end
# EMP_40 tag
if product.tags.include?(TAG_EMP_40)
DISCOUNT = ((100 - DISCOUNT_PERCENTAGE_40) / 100)
line_item.change_line_price(line_item.line_price * DISCOUNT, message: DISCOUNT_MESSAGE_40) #Apply the overall discount to the line-item.
if Input.cart.discount_code != nil
is_discount_applied = true
end
end
end
end
if is_discount_applied
Input.cart.discount_code.reject({
message: CART_REJECTION_MESSAGE
})
end
Output.cart = Input.cart
Any help is greatly appriciated.
Thanks in advance.