This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Shipping Script to provide free shipping based on customer tags | |
class FreeShippingScript | |
def initialize(cart, shipping_rates) | |
@cart = cart | |
@shipping_rates = shipping_rates | |
@customer = cart.customer | |
end | |
def run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Input.cart.line_items.each do |line_item| | |
product = line_item.variant.product | |
max_qty_tag = product.tags.find { |tag| tag.start_with?('max_') } | |
if max_qty_tag | |
max_qty = max_qty_tag.gsub('max_', '').to_i | |
reduce_by = line_item.quantity - max_qty | |
line_item.split(take: reduce_by) if reduce_by > 0 | |
end | |
end |