Skip to content

Instantly share code, notes, and snippets.

View peterberwind's full-sized avatar

Peterberwind peterberwind

View GitHub Profile
### Keybase proof
I hereby claim:
* I am peterberwind on github.
* I am peterberwind (https://keybase.io/peterberwind) on keybase.
* I have a public key ASB5NIXHKff9gtNkMDxdV4BXNpui4jOhJ2tiabZcoACNkgo
To claim this, I am signing this object:
discounted_product = 12275195905
products_needed = [592406273]
products_seen = []
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
products_seen << product.id if products_needed.include?(product.id)
end
Input.cart.line_items.each do |line_item|
@peterberwind
peterberwind / gist:405db2d4c546e9990d4e9601ab72d348
Created May 3, 2017 20:42
Have purchased at least X orders, Get $Y off
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count > 2 #number of orders placed to get the deal
discount = 1000 #discount amount in cents
message = "VIP Customer - $10 off"
end
end
puts discount
@peterberwind
peterberwind / gist:d9c53609cbbcd11567975d516c9d42e0
Created May 3, 2017 20:41
Have purchased at least X orders, Get Y% off
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count > 2 #number of orders needed to get discount
discount = 0.2 #percent discount in decimal form
message = "VIP Customer"
end
end
puts discount
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count == 0
discount = 0.1 #change the discount given here
message = "Thanks for placing your first order" #change the message shown here
end
puts discount
customer = Input.cart.customer
discount = 0
message = ""
if customer
if customer.orders_count < 1
discount = 1000 #discount amount in cents
message = "New Customer - $10 off"
end
end
puts discount
@peterberwind
peterberwind / gist:ea041e4176c1dadc3d1e519114cd1983
Created May 3, 2017 20:41
X% off all products with Y tag
@percent = Decimal.new(25) / 100.0
Input.cart.line_items.each do |line_item|
product = line_item.variant.product
next if product.gift_card?
next unless product.tags.include?('myTag')
line_discount = line_item.line_price * @percent
line_item.change_line_price(line_item.line_price - line_discount, message: "25% Off")
end
Output.cart = Input.cart
@peterberwind
peterberwind / gist:383a5049a69d8f34756204ea745f689c
Created May 3, 2017 20:40
Buy X quantity of a product, Get Y% off
DISCOUNTS_BY_QUANTITY = {
10_000 => 20,
1_000 => 15,
100 => 10,
10 => 5,
}
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
@peterberwind
peterberwind / gist:f7404e07292cd5787b6e29f0c9998046
Created May 3, 2017 20:40
Buy X quantity of a product, Get $Y off
DISCOUNTS_BY_QUANTITY = {
10_000 => 20,
1_000 => 15,
100 => 10,
10 => 5,
}
Input.cart.line_items.each do |line_item|
next if line_item.variant.product.gift_card?
PAID_ITEM_COUNT = 1
DISCOUNTED_ITEM_COUNT = 1
# Returns the integer amount of items that must be discounted next
# given the amount of items seen
#
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