As a background, what we're trying to do at a base level is determine a Discounts percentage off, then use that to only allow a discount on the Compare at Price (Full Price) and not the regular price. The [simplified] code is as follows:
if Input.cart.discount_code case Input.cart.discount_code when CartDiscount::Percentage discount = Input.cart.discount_code.percentage Input.cart.line_items.each do |item| if item.variant.instance_variable_defined?(:@compare_at_price) line_price = item.variant.compare_at_price * item.quantity else line_price = item.line_price end item.change_line_price(line_price * ((100.0 - discount) / 100.0), message: "Applied") end Input.cart.discount_code.reject({ message: 'Discount Applied to Select Products' }) end end
But this creates the error "[Error] non float value" on the line:
item.change_line_price(line_price * ((100.0 - discount) / 100.0), message: "Applied")
Unfortunately, as far as I can see through my extensive research, there is no way in the Ruby that Shopify uses to turn "discount" into a float value [Ruby would allow something like "discount.to_f"]. I could not figure out any other way than to do the following:
item.change_line_price(line_price * ((100.0 - int_float(discount.to_s)) / 100.0), message: "Applied")
and create the function:
def int_float(int) case int when "5.0" 5 when "10.0" 10 when "15.0" 15 when "20.0" 20 when "25.0" 25 when "30.0" 30 when "35.0" 35 when "40.0" 40 when "45.0" 45 when "50.0" 50 when "55.0" 55 when "60.0" 60 when "65.0" 65 when "70.0" 70 when "75.0" 75 when "80.0" 80 when "85.0" 85 when "90.0" 90 when "95.0" 95 end end
Which works, but is cumbersome. If someone could help me understand either what I did wrong in trying to use that percentage, or if my calculations need to occur differently, I'd like to make this code smarter.
Any help is appreciated!
Aaron
Solved! Go to the solution
That's odd. What error specifically do you get if you use discount.to_f or discount.to_d? Have you tried to fool it by doing discount * 1.0?
Thanks for the reply, SSv!
It is weird, right? So here's what occurs in the following scenarios:
discount.to_f gets the error "[Error] undefined method 'to_f'"
discount.to_d repeats the original error of "[Error] non float value"
discount * 1.0 repeats the original error of "[Error] non float value"
I had also tried using a combination of turning it into money and doing some math with it but nothing stuck. Odd stuff indeed. My math formula isn't at fault is it?
This is an accepted solution.
OK, that was intense :)) but I made it through, below is the code that should do it:
if Input.cart.discount_code case Input.cart.discount_code when CartDiscount::Percentage discount = Input.cart.discount_code.percentage Input.cart.line_items.each do |item| item.change_line_price(item.line_price * ((Decimal.new(100) - discount) / 100), message: "Applied") end Input.cart.discount_code.reject({ message: 'Discount Applied to Select Products' }) end end Output.cart = Input.cart
I've also removed the if logic, not sure what you are trying to do, line_price already has the total value considering quantity.
Very interesting find there! Not sure what the logic is behind that, but maybe it was thinking that the 100 I was subtracting from was a float. Makes no sense to me why it would operate that way, but I'm no Ruby expert!
You are the best, and thanks for that help, I'll keep that in mind for future scripts!
Oh, and the reason it had the logic there was because we are attempting to take the percentage off of the Original Price, and not the Sale Price (the idea being that if someone wants to use a discount code, we only give them the discount on Retail Price, and not Sale Price) so we had to determine a new "line_price" in that sense.
Biggest of thanks
Aaron
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |