">

Shopify Scripts - Buy One Get One x% Off BOGO Discounts

Feb. 14, 2018
Shopify brand identity eCommerce marketing
“ This script applies your BOGO discount to any item in your customers cart of equal or lesser value!”

In the world of online clothing retail typical BOGO discounts don't get the job done. Unless your customers are going for the Steve Jobs wardrobe aesthetic, they aren't going to get excited for your sale when your discount only allows them to purchase two of the exact same t-shirts or jeans. Introducing a BOGO Shopify Script written with eCommerce fashion in mind (though I'm sure it will suit a variety of industries needs).


def partition (cart, line_items)
  sorted_items = line_items.sort_by{|line_item| line_item.variant.price}.reverse
  discounted_items = []
  
  sorted_items.each do |line_item|
    count = sorted_items.quantity /2
    next if count <= 0
    if count >= line_item.quantity
      # If the full item quantity must be discounted, add it to the items to return
      # and increment the count of discounted items
      discounted_items.push(line_item)
      discounted_items_seen += line_item.quantity
    else
      # If only part of the item must be discounted, split the item
      discounted_item = line_item.split(take: count)
      # Insert the newly-created item in the cart, right after the original item
      position = cart.line_items.find_index(line_item)
      cart.line_items.insert(position + 1, discounted_item)
      # Add it to the list of items to return
      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? 
end

discounted_line_items = partition(Input.cart, eligible_items)
discounted_line_items.each do |line_item|
  new_line_price = line_item.line_price * 0.5
  line_item.change_line_price(new_line_price, message: "Buy One, Get One 50% Off")
end


Output.cart = Input.cart

  • Something to say
  • Listing things in a list
  • Pay close attention to number 4
  • What happens next will shock you

Let's begin

Start your project