Now that Ive read a bit more about the cart cupon code on the Script Editor, Im sure I can make this happen. Im just unsure how. I want to create specific curpon codes for more then 100 people who will be getting a free gift from us for them to try out our service. All they have to do is add the product and use the cupon. But the cupon with a percentage discount acitvates on ALL products with enter the criteria I use, which in this case is if they are from a certain collection. What we would like to do is put the 100% discount only on the first item it finds. Do you guys thinks this is possible? Any ideas where or how I could start this out? Im still investigating how it could be done and for what Ive seen maybe I could use the script editor to apply once a cupon has been used which will have a prefix with which I could catch to make the discount only affect the first product. Could anyone give me an idea of how the code would be? (Im not really good with ruby hehe D: All though Im learning right now.) If you need any more information please do ask :D Ill be happy to responde.
Is there a way of knowing if the code discount is applying to more then 1 product? Cause I could just reject the cupon if the customer tries to add more then 1 product with the same discount? Is this possbile?
Ok so I think I found two ways. Its not the best ways I suppose but at least its something.
The first way is by rejecting the code if there are two or more products of the same vendor or type. This will help you make the customer only buy one, but the problem here is he wont buy more then one, it would only be the gift. Here is the code:
case Input.cart.discount_code <%-# I use the discount code percentage, but you could change it to any other -%> when CartDiscount::Percentage <%-# get the discount code -%> discount_code_string = Input.cart.discount_code.code <%-# I use a prefix in the cupon codes to know if I should check it out -%> if discount_code_string.include? "regalopanales" <%-# use a flag to know if there are more then two different products from the same vendor -%> discount_flag = true for item in Input.cart.line_items <%-# I check out if the item is from a certain vendor -%> if item.variant.product.vendor == "Huggies Supreme" || item.variant.product.vendor == "Huggies Ultraconfort" <%-# check if flag has already been used -%> if discount_flag <%-# check if there are more then 1 product -%> if item.quantity > 1 <%-# If there is, I reject the cupon -%> Input.cart.discount_code.reject(message: "Codigo nada mas aplica para 1 producto") end <%-# activate flag so if another product with the same vendor comes in, its checked -%> discount_flag = false else <%-# When there are two different products from the same vendor -%> Input.cart.discount_code.reject(message: "Codigo nada mas aplica para 1 producto") end end end end end Output.cart = Input.cart
The other way is to create a cupon which is worth 0% and do the discount on the cart yourself:
case Input.cart.discount_code when CartDiscount::Percentage discount_code_string = Input.cart.discount_code.code if discount_code_string.include? "regalopanales" puts Input.cart.discount_code.percentage discount_flag = true for item in Input.cart.line_items if item.variant.product.vendor == "Huggies Supreme" || item.variant.product.vendor == "Huggies Ultraconfort" if discount_flag puts item.line_price item.change_line_price(item.line_price - item.variant.price, message: "Se aplico descuento del cupon") discount_flag = false end end end end end Output.cart = Input.cart
This way it applies the discount on the first product which should have the discount.
I hope this helps anyone hehe if you need help understanting my code, do ask :D Ill gladly help out. And if you see any errors that might come in using the code I did, please do tell me haha. Im still learning how ruby works and the Script Editors limits as well.
The code is not done yet. Right now Ill try to apply the discount on the product which is worth more. But still havent found a solution. Help is much appriciated :P
Ok so I coded a way to create your own cupon code based on another cupon code which is set to 0% discount haha. Its not the propper wasy, but it works as far as Ive tested. Ill list a few tests Im going to run which I think might break the code Ive done and give the answers later haha. So far this is the code Ive built:
case Input.cart.discount_code when CartDiscount::Percentage discount_code_string = Input.cart.discount_code.code if discount_code_string.include? "regalopanales" discount_flag = false item_highest_price = Money.zero item_highest_id = 0 for item in Input.cart.line_items if item.variant.product.vendor == "Huggies Supreme" || item.variant.product.vendor == "Huggies Ultraconfort" if item_highest_price < item.variant.price unless item.variant.product.tags.include?("Cajas Pañales") puts item.variant.product.tags item_highest_id = item.variant.id item_highest_price = item.variant.price discount_flag = true end end end end if discount_flag for item in Input.cart.line_items if item.variant.id == item_highest_id item.change_line_price(item.line_price - item.variant.price, message: "Se aplico descuento del cupon") end end end end end Output.cart = Input.cart
I ran 2 for loops. The first finds the id with the highest price on the items who are from the vendor Im looking for. Notice this can be changed to tags or product_type. I also used a UNLESS which I rule out products with a ceratain tag ("Cajas Pañales"). With this three you can preatty much select a group of products which you want to add the discount to. In this case I discount only once 100% discount on the highest cost item.
A few test Ive got to run are the next:
1. What if the client adds the cupon then goes back to the store then back to the checkout to adds the cupon again. My theory here is the discount will be afected two times.
2. What if the client delets the cupon and puts it agian.
For either of this I could add another for before the first for loop and check if there is a discount with the message of the cupon. But I want tom make sure this will happen. :P Ill test it out and post the result again jaja. If anyone sees a way to optimize my code :D All observations are welcome. Im new to Ruby so if my code is kind of ugly, thats the reason haha Im learing. :P Well Ill keep it poseted here.
So I made the tests mentioned before and the code works 100% :D If the cupon is removes the discount is removed as well. And if the person tries going back to the store the discount gets removed as well. So this code works perfectly well. If anyone can think of a test or a hole I might be missing where a person will be able to use this cupon several times. Please do tell me what to test it against haha. I hope this codes help anyone who need it :P If anyone needs help using the Script Editor in a different way Ill gladly help to the best of my ability! :D Its worth noting that, at least in my theme, on the checkout the message of the product having a discount is not shown. Im searching why this might be :P
Good Luck Everyone! :D