Percentage discount script

I have a script that applies a percentage discount to the price according to a specific tag. How would I be able to use that script with the shopify function?

script just in case:

customer = Input.cart.customer

#if logged in
if customer

if tagged freeitems

if customer.tags.include?(‘freeitems’)
Input.cart.line_items.each do |item|
item.change_line_price(item.line_price * 0.0, message: “”)
end
elsif customer.tags.include?(‘school’)
#each product
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
product.tags.each do |tag|
if tag.include? ‘b2b’
per = tag.split(‘:’)[1].split(‘%’)
percent = per[0].to_i
PERCENTAGE = (100-percent)/100
MESSAGE = per[0] + ‘% off’
line_item.change_line_price(line_item.line_price * PERCENTAGE, message: MESSAGE)
end
end
end
end
else
puts “Something went wrong”
end
Output.cart = Input.cart

You can access customer and product tags as part of the input query, however you will need to know which tags to check for. Customer tags are available at cart.buyerIdentity.customer.hasTags (or hasAnyTag) and product tags are within the merchandise object.

If your goal is to apply B2B-specific discounts with a different value per product, you should take a look at using Catalogs for B2B pricing. Alternatively, you could encode these data using a product metafield - this would allow you to have a consistent key to check for and you could store the value in a meaningfully typed way without needing to do string processing.

You might also want to try asking Dev Docs Assistant for help on this, as it can likely generate code that will serve your specific need.