Created
February 4, 2017 05:42
-
-
Save seedcms/ad4a757e3aff2ee49a809f186fd878fb to your computer and use it in GitHub Desktop.
Combined script 1
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
money = Money.zero | |
PAID_ITEM_COUNT = 1 | |
DISCOUNTED_ITEM_COUNT = 1 | |
def discounted_items_to_find(total_items_seen, discounted_items_seen) | |
Integer(total_items_seen / (PAID_ITEM_COUNT + DISCOUNTED_ITEM_COUNT) * DISCOUNTED_ITEM_COUNT) - discounted_items_seen | |
end | |
def partition(cart, line_items) | |
sorted_items = line_items.sort_by{|line_item| line_item.variant.price}.reverse | |
discounted_items = [] | |
total_items_seen = 0 | |
discounted_items_seen = 0 | |
sorted_items.each do |line_item| | |
total_items_seen += line_item.quantity | |
count = discounted_items_to_find(total_items_seen, discounted_items_seen) | |
next if count <= 0 | |
if count >= line_item.quantity | |
discounted_items.push(line_item) | |
discounted_items_seen += line_item.quantity | |
else | |
discounted_item = line_item.split(take: count) | |
position = cart.line_items.find_index(line_item) | |
cart.line_items.insert(position + 1, discounted_item) | |
discounted_items.push(discounted_item) | |
discounted_items_seen += discounted_item.quantity | |
end | |
end | |
discounted_items | |
end | |
eligible_items = Input.cart.line_items.select do |line_item| | |
product = line_item.variant.product | |
product.tags.include?('BOGO') | |
end | |
Input.cart.line_items.each do |line_item| | |
cart = Input.cart | |
product = line_item.variant.product | |
money+=line_item.line_price | |
if cart.discount_code.code == "XYZ" | |
if product.tags.include?('shoes') | |
money-=line_item.line_price | |
end | |
end | |
end | |
Input.cart.line_items.each do |line_item| | |
puts money | |
cart = Input.cart | |
product = line_item.variant.product | |
if cart.discount_code.code == "XYZ" | |
unless product.tags.include?('shoes') | |
if money >= Money.new(cents: 3000) | |
line_item.change_line_price(line_item.line_price * 0.90, message: "XYZ") | |
else | |
cart.discount_code.reject({ message: "Products excluding XYZ not over required amount" }) | |
end | |
end | |
puts money | |
elsif cart.discount_code.code == "OVER" | |
if money >= Money.new(cents: 5000) | |
if product.id == 9284928268 | |
if line_item.quantity > 1 | |
new_line_item = line_item.split(take: 1) | |
new_line_item.change_line_price(Money.zero, message: "OVER") | |
cart.line_items << new_line_item | |
else | |
line_item.change_line_price(Money.zero, message: "OVER") | |
end | |
end | |
else | |
cart.discount_code.reject({ message: "Products not over required amount" }) | |
end | |
elsif cart.discount_code.code == "BOGO" | |
discounted_line_items = partition(Input.cart, eligible_items) | |
discounted_line_items.each do |line_item| | |
line_item.change_line_price(Money.zero, message: "BOGO") | |
end | |
end | |
end | |
Output.cart = Input.cart |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment