Percentage discount script

Topic summary

A user seeks guidance on migrating a Shopify Script that applies percentage discounts based on customer and product tags to the newer Shopify Functions API.

Original Script Behavior:

  • Checks if customer has ‘freeitems’ tag → applies 100% discount
  • Checks if customer has ‘school’ tag → parses product tags containing ‘b2b:X%’ format and applies corresponding percentage discount

Migration Guidance Provided:

  • Customer tags accessible via cart.buyerIdentity.customer.hasTags or hasAnyTag
  • Product tags available within the merchandise object
  • Tag names must be known in advance for querying

Alternative Approaches Suggested:

  • Use Catalogs for B2B pricing for product-specific B2B discounts (native Shopify feature)
  • Store discount percentages in product metafields instead of tags for cleaner data handling and to avoid string parsing
  • Try Dev Docs Assistant for generating function-specific code

The discussion remains open with no confirmation of which approach the user will pursue.

Summarized with AI on October 24. AI used: claude-sonnet-4-5-20250929.

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.