This post uses Shopify Scripts API to create the Buy One Get One, generally called BOGO functionality. If you want to create Buy One and get Second Item on 50% off (the discount will be applied to the least priced item), like Pela Case is doing, this code will work as well.
Shopify has already created a script for BOGO and Percentage off least expensive item but the Percentage off least expensive item script is buggy. It does not give a discount if you add the same item to cart twice because
Input.cart.line_items.size counts it as just one item. So, we have to count the quantity of the same product manually.
The problem has been fixed in this code:
You can change the
DISCOUNT_AMOUNT variable to 100 to give 100% on the second item, which is Bug One Get One Item free. In Shopify’s BOGO script, we have to mention the products IDs and it works for different products. But this method works on all the products irrespective of the IDs
Shopify Scripts API Documentation is worth checking if you looking to create a different functionality. Shopify scripts use Ruby programming language, so if you want to try your hands on Ruby, follow this link.