Skip to content

Instantly share code, notes, and snippets.

@juiceByStaplez
Created December 22, 2016 04:37
Show Gist options
  • Save juiceByStaplez/a15f2f5958c36a6c5adfe241a4f4c257 to your computer and use it in GitHub Desktop.
Save juiceByStaplez/a15f2f5958c36a6c5adfe241a4f4c257 to your computer and use it in GitHub Desktop.
# A class that knows how to calculate the discount for a line item, given a
# number of cents to discount each unit of item by.
class MoneyDiscount
def initialize(cents)
@amount = Money.new(cents: cents)
end
def calculate(line_item)
@amount * line_item.quantity
end
end
# A class that knows how to calculate the discount for a line item, given a
# number representing the percentage to discount each unit of item by.
class PercentageDiscount
def initialize(percent)
# Calculate the percentage, while ensuring that Decimal values are used in
# order to maintain precision.
@percent = Decimal.new(percent) / 100.0
end
def calculate(line_item)
line_item.line_price * @percent
end
end
# A class that knows how to apply a discount over a cart, for line items that
# match a certain product ID.
# A class that knows how to apply a discount over a cart, for line items that
# match a certain product ID.
class BundleDiscountCampaign
# Initialize the instance with the discounted product ID, the Discount
# instance that will be used to calculate the discount, and the message that
# will be used to describe the discount.
def initialize(product_type, discount, message)
@product_type = product_type
@discount = discount
@message = message
@bundle_count = 0
@free_items_count = 0
end
# Apply the discount to a given Cart. Also prints debugging lines to the
# console with the variant IDs of the line items being discounted and the
# discount amounts being applied.
def run(cart)
case cart.discount_code
when CartDiscount::Percentage
@discount = PercentageDiscount.new(cart.discount_code.percentage)
when CartDiscount::FixedAmount
@discount = MoneyDiscount.new(cart.discount_code.amount.cents)
else
end
cart.line_items.each do |line_item|
# Skip this line item unless it's associated with the target product.
next unless !line_item.properties.key?('gcm_recipient_email')
# Using the Discount instance, calculate the discount for this line item.
line_discount = @discount.calculate(line_item)
# Calculated the discounted line price using the line discount.
new_line_price = line_item.line_price - line_discount
# Apply the new line price to this line item with a given message
# describing the discount, which may be displayed in cart pages and
# confirmation emails to describe the applied discount.
line_item.change_line_price(new_line_price, message: @message)
# Print a debugging line to the console.
puts "Discounted line item with variant #{line_item.variant.id} by #{line_discount}."
@bundle_count = @bundle_count - 1 * line_item.quantity
end
end
end
if Input.cart.discount_code
code = Input.cart.discount_code
#Input.cart.discount_code.reject(message:'Gift Card Discount applied')
end
# Use an array to keep track of the discount campaigns desired.
CAMPAIGNS = [
BundleDiscountCampaign.new(
'',
Input.cart.discount_code,
"Gift Card Check",
),
]
if code
puts code
# Iterate through each of the discount campaigns.
CAMPAIGNS.each do |campaign|
# Apply the campaign onto the cart.
campaign.run(Input.cart)
end
end
# In order to have the changes to the line items be reflected, the output of
# the script needs to be specified.
Output.cart = Input.cart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment