Shopify Scripts
Shopify Scripts
买指定商品,享受所有配件八折,其中,部分配件仅允许享受一次八折优惠
SPECIFIC_VID = 28785311875136 UNLIMIT_DISCOUNT_PIDS = [3742304010304, 3733881094208] LIMIT_ONCE_DISCOUNT_PIDS = [3742295982144] def partition(cart, line_items) # Sort the items by price from high to low sorted_items = line_items.sort_by{|line_item| line_item.variant.price}.reverse # Create an array of items to return discount_items = [] sorted_items.each do |line_item| if LIMIT_ONCE_DISCOUNT_PIDS.include?(line_item.variant.product.id) && line_item.quantity > 1 discount_item = line_item.split(take: 1) cart.line_items << discount_item discount_items.push(discount_item) else discount_items.push(line_item) end end discount_items end Input.cart.line_items.each do |line_item| next unless line_item.variant.id == SPECIFIC_VID eligible_items = Input.cart.line_items.select do |line_item| product = line_item.variant.product UNLIMIT_DISCOUNT_PIDS.include?(product.id) || LIMIT_ONCE_DISCOUNT_PIDS.include?(product.id) end discount_line_items = partition(Input.cart, eligible_items) discount_line_items.each do |line_item| line_item.change_line_price(line_item.line_price * 0.80, message: "Hiahiahia") end end Output.cart = Input.cart
满足条件则Standard免邮
flag = false if Input.cart.subtotal_price >= Money.new(cents: 5000) flag = true end fs_product_ids = [3852672368704, 3852680659008] Input.cart.line_items.each do |line_item| next unless fs_product_ids.include?(line_item.variant.product.id) flag = true end if flag Input.shipping_rates.each do |shipping_rate| next unless shipping_rate.name == "Standard" shipping_rate.apply_discount((shipping_rate.price * 1), message: "Free shipping") end Output.shipping_rates = Input.shipping_rates else Output.shipping_rates = Input.shipping_rates end
复杂优惠组合:第二个半价,达到数量赠送部分,达到金额赠送礼品
#Part1, Kit Second Half Price num=0 Input.cart.line_items.each do |line_item| next unless line_item.variant.product.id==1869754073152 if(line_item.quantity >= 2) then dnum=(line_item.quantity).div(2) line_item.change_line_price(line_item.line_price - line_item.variant.price * 0.5 * dnum, message: "Kit Second Half Price") num=line_item.quantity % 2 else num+=line_item.quantity next unless num==2 line_item.change_line_price(line_item.line_price * 0.5, message: "Kit Second Half Price") num=0 end end #Part2, Pods Buy 10 Get 2 Free PAID_ITEM_COUNT = 10 DISCOUNTED_ITEM_COUNT = 2 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.gift_card? && product.id == 1880855576640 end discounted_line_items = partition(Input.cart, eligible_items) discounted_line_items.each do |line_item| line_item.change_line_price(Money.zero, message: "RelxPods Buy 10 Get 2 Free") end #Part3, Purchase Over 199 Get Gift FREE_GIFT_PRODUCT_ID = 4168668315712 if Input.cart.subtotal_price >= Money.new(cents: 19900) Input.cart.line_items.each do |line_item| if line_item.variant.product.id == FREE_GIFT_PRODUCT_ID if line_item.quantity > 1 line_item = line_item.split(take: 1) Input.cart.line_items << line_item end line_item.change_line_price(Money.zero, message: "Free Gift With Purchase Over 199") end end end Output.cart = Input.cart