Personalized checkout and custom promotions with Shopify Scripts
I would like to check the amount of a shipping rate and see what it ends with, so I can then discount a set amount.
Basically I want to be able to do modulo math. Like
if shipping_rate.price.cents % 100 == 95
# discount specific amount
But I can't find any way to get a range of digits from a Money object in Shopify Scripts. And I don't see how to convert a Money object to an integer.
My use case: we have a bunch of shipping rates for differing amounts, some from apps using carrier calculated rates, some from Shopify shipping profiles. We want to discount only the amount of the Shopify provided rates, not the app provided rates.
The problem is when rates are merged ("source: Shopify"). I can detect when merged, but I can't discount just the app provided rates.
There's a bunch of different instances where we may or may not want to apply a discount, primarily cart thresholds. I can figure it out using some simple modulo math due to how are rates are set up — but I can't figure out how to access those final two digits within a Shopify Script.
How do I convert a Money object to an integer or compare to just certain digits in a money object?
Solved with few conversions. `cents` is a Decimal. I converted `to_s` and then `to_i`.
Hacky, but looks like this:
last_two = shipping_rate.price.cents.to_s.to_i % 100
Thanks for the information!
If you're looking to calculate shipping rates and apply discounts based on the final digits, modulo math is the way to go! It allows you to determine the remainder after dividing a number, which can be useful for your purpose. By using the modulo operator, you can easily extract the last digits of the shipping rate, enabling you to apply targeted discounts accordingly. To streamline the process, consider exploring online tools or programming languages that support modulo operations. For a comprehensive understanding and practical implementation, resources like https://casinoplinko.com could provide valuable insights into utilizing modulo math effectively for your shipping rate calculations and discount strategies. Happy shipping and discounting!
Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024