Using Money in the message of a discount script

David_Leininger
New Member
4 0 0

One of the discounts I'm working on takes off $0.75 per item. I'd like to tell the customer their total savings in the message string. This works fine, but right now if the person saved $7.50 it says, "You saved $7.5!" I'd like it to say, "You saved $7.50!" or "You saved $8.25" or "You saved $3.00" or any other variation.

Right now, If I add a value that is a money object I get this: "You saved $#<Money: "7.75$">!"

Any thoughts on how to make this happen.

0 Likes
Jason
Shopify Expert
10039 119 1876

Hey David,

Looks like this post got a little lost in the xmas madness. Money will remove those trailing zeros and the current implementation doesn't support formatting. You could look at modifying the string to add the zeros.

I'm not even close to being decent at Ruby so expect the below to be re-written so it's more efficient / concise. Take this example:

a = 4.10
b = 11
LinePrice = a * b
lineDivmod = LinePrice.divmod 1
lineDivmod[1] = lineDivmod[1].round(2).to_s.split('.').last.ljust(2, '0')
puts lineDivmod.join(".")

That should output 45.10 rather than 45.1.

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
David_Leininger
New Member
4 0 0

I'm not sure what the problem is. It seems like regular Ruby stuff isn't working. Don't get me wrong, I'm pretty new to Ruby, but nothing seems to work correct. Almost everying, including .divmod is throwing an error in the console ([Error] undefined method 'divmod' for #<Decimal:0x7fa944740b80>).

Again, it could be that I'm new to this, but I've looked around at how to use two decimal places in Ruby, and those never work.

Basically here is what I'm trying to do:

# Calculated the discounted line price using the line discount.
discount_price = line_item.line_price - (Money.new(cents:100) * discount * line_item.quantity)
  
#Caculate Total Discount
total_discount = Decimal.new(discount * line_item.quantity)
  
# Set Message.
message = "$#{discount} off per item when buying more than #{quantity - 1}. You saved $#{total_discount}!"
  
line_item.change_line_price(
  discount_price,
  message: message,
)

I'm not trying to use Money anymore. I'm just trying to figure out how to get the message to ready "You saved $X.XX!"

I've tried rounding. I've tried doing total_discount + 0.001, and then using .round(2). That just rounds to a whole number.

0 Likes
HunkyBill
Shopify Expert
4372 36 467

An amount of money... is 

Money.new(cents: cents)

where cents is going to be 100. Like you have. So that is one dollar. So multiplying by 10 is going to be ten dollars. Looking at your line about total_discount, don't use that. Whatever discount is, it is likely the source of your problems. Ruby handles decimals well without any special treatment, but you're better off staying with the Shopify Money object for most things... 

And note that as far as I know, at this time, those messages are never shown to any customers anyway, as Shopify has yet to expose them anywhere other than the Script Editor itself. So you're gassing yourself for nothing. All good learning though!!

 

 

Custom Shopify Apps built just for you! hunkybill@gmail.com http://www.resistorsoftware.com
0 Likes