Created
May 21, 2024 20:15
-
-
Save westondeboer/6065b81ba67544ef542d8710e38c1b5f to your computer and use it in GitHub Desktop.
Free Shipping for VIP customers
This file contains hidden or 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 | |
# Replace 'vip' with the tag you want to check | |
if @customer && customer_has_tag?('vip') | |
apply_free_shipping | |
end | |
end | |
private | |
def customer_has_tag?(tag) | |
@customer.tags.include?(tag) | |
end | |
def apply_free_shipping | |
@shipping_rates.each do |shipping_rate| | |
shipping_rate.apply_discount(shipping_rate.price, message: "Free shipping for VIP customers") | |
end | |
end | |
end | |
# Main execution | |
script = FreeShippingScript.new(Input.cart, Input.shipping_rates) | |
script.run | |
Output.shipping_rates = Input.shipping_rates |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Free shipping for customers who are tagged with vip