Hello - I have a basic Question about Shopify Function.
We have a Script in one Shop which applies a fixed Product Price Discount based on a Product Tag which contains the quantity and the Price. This works very well on Shopify Scripts.
Now we try to get this logic to Functions. But every attempt failed. Is this logic possible with functions? The Script looks like this:
TIERED_PRICING_TAG = 'TIERED_'
TIEREDPRICE_PRICING_TAG = 'TIEREDPRICE_'
class TieredPricingCampaign
def initialize(product_tag)
@product_tag = product_tag.downcase
end
def run(line_item, cart)
# start with no price adjustment
final_amount, message = nil, nil
# look at each product tag
line_item.variant.product.tags.each do |tag|
products_tag = tag.downcase
# if the tag matches our campaign, then extract the qty / discount amount
if (products_tag.split(@product_tag)[0] === '')
needed_qty, discount = products_tag.split(@product_tag)[1].split('_')
# if they bought enough, then we calculate the new price
if line_item.quantity >= needed_qty.to_i
new_amount = Money.new(cents: line_item.line_price.cents * ((100 - discount.to_i) / 100))
new_message = "ab #{needed_qty} Stück - #{discount}% sparen"
# if this is a better deal than whatever we have so far, use it instead
if !final_amount || final_amount.cents > new_amount.cents
final_amount = new_amount
message = new_message
end
end
end
end
# if we didn't find any matching tags, then we return nil
return final_amount, message
end
end
class TieredPricePricingCampaign
def initialize(product_tag)
@product_tag = product_tag.downcase
end
def run(line_item, cart)
# start with no price adjustment
final_amount, message = nil, nil
# look at each product tag
line_item.variant.product.tags.each do |tag|
products_tag = tag.downcase
# if the tag matches our campaign, then extract the qty / discount amount
if (products_tag.split(@product_tag)[0] === '')
needed_qty, discount = products_tag.split(@product_tag)[1].split('_')
# if they bought enough, then we calculate the new price
if line_item.quantity >= needed_qty.to_i
new_amount = Money.new(cents: 100 * ((line_item.quantity * discount.to_f) ))
new_message = "ab #{needed_qty} Stück - je #{discount} €/Stück"
# if this is a better deal than whatever we have so far, use it instead
if !final_amount || final_amount.cents > new_amount.cents
final_amount = new_amount
message = new_message
end
end
end
end
# if we didn't find any matching tags, then we return nil
return final_amount, message
end
end
# Runs each campaign, whichever returns the lowest price is applied to the line item
class MaximumDiscountCampaign
def initialize(campaigns)
@campaigns = campaigns
end
def run(cart)
cart.line_items.each do |line_item|
min_price = line_item.line_price
final_message = nil
@campaigns.each do |campaign|
new_price, message = campaign.run(line_item, cart)
if new_price
if new_price.cents < min_price.cents
min_price = new_price
final_message = message
end
end
end
if (min_price.cents < line_item.line_price.cents && final_message)
line_item.change_line_price(min_price, message: final_message)
end
end
end
end
CAMPAIGNS = [
TieredPricingCampaign.new(TIERED_PRICING_TAG),
TieredPricePricingCampaign.new(TIEREDPRICE_PRICING_TAG)
]
campaign = MaximumDiscountCampaign.new(CAMPAIGNS)
campaign.run Input.cart
Output.cart = Input.cart```