Apply discount to most expensive item in cart

Tourist
27 0 2

This is great! the concept I was missing was assigning the entity at index 0 to a variable, then manipulating that variable. Your example worked perfectly and simplified the process considerably by removing the bubble sort method I had created.

As a case study though, usage within the script testor reported zero change in resource allocation (CPU @ 1% and RAM @ 7%) between your method and my method.  Is there a best practices to be learned in this comparative example?

0 Likes
Explorer
296 0 24

I don't know.. these are my first lines of code in Ruby..

But I know one thing for sure.. If this

Input.cart.line_items.each do |line_item|
  if high == line_item
    line_item.change_line_price(high.variant.price * 0.80, message: "discount")
  end
end

can be changed to something like this

Input.cart.line_items[some_index_value_variable].change_line_price(high.variant.price * 0.80, message: "discount")

The CPU usage and RAM will be lower.. I couldn't get it to work..

I make apps and can help you in setting up payment gateways..
0 Likes
New Member
4 0 0

Do you know how to add a discount code requirement to this?

 

Input.cart.line_items.each do |line_item| if high == line_item line_item.change_line_price(high.variant.price * 0.80, message: "discount") end end

I'd like to be able to require the discount code so I can prevent my customers from placing multiple orders and abusing the discount. 

 

0 Likes
Tourist
27 0 2

are you referring to the code requirements you set when you create a code through the shopify admin area? things like single use per customer(based on email address), or only works on certain products/collections etc.?

If this is what you are referring to, the code inherits these parameters as well as the new script parameters.  So for the example above it checks to see if the customer has used the code before(based on email), it then checks if the products in the cart match the criteria set in the admin panel, then it checks for the highest priced item that it can apply to and performs its discount.

The first two checks mentioned above where set in the admin area, and the last two actions where specified in the script.

Please let me know if I can help clarify, or if I'm not answering your question correctly.

0 Likes
New Member
4 0 0

Hi A-aron, 

What I'm trying to do is add the discount code requirement to the script. 

I know I have to create the discount in the shopify admin, limiting it once per customer by email but I don't know the code I need to add to require a discount code to apply the script discount. 

The current code automatically applies the discount at checkout without the customer entering a discount code. 

I have pretty much zero knowledge in terms of writing the script, I actually found the code to discount just the higer item from this thread. 

0 Likes
Tourist
27 0 2

ahhh so you need to basically wrap the discount portion of your code in an IF statement...then grab the discount applied to the cart, and in the if statement:

if (discount code found = discount code expected) do discount

You can get the discount code being applied to the cart using this:

if Input.cart.discount_code && discount_names.include?(Input.cart.discount_code.code)

Where discount_names is an array of all discount codes that can enable your discount script.  To explain the code above Input.cart.discount_code simply looks up if the code is present.  everything after the && checks if the predefined codes match the one that was just provided in the cart.

0 Likes
New Member
4 0 0

I'm such a newb - any chance you can post what the completed script would be? I have no idea in what section of the script I'm supossed to even put it or where to put the specific code I want to use. 
I've been googling this for what feels like forever - I always try to figure things out on my own, but I'm at a loss. 

Any and all help super appreciated. 

0 Likes
Highlighted
Tourist
27 0 2

I'm very new to ruby and don't want to make my hacked code into a template for the most used version as more people come across it.  Fill the internet with solid code, and not my learning mess.

Think of it this way though:

1) set up an array or object that is equal to the value that would allow the discount to work (discount_names=[discountcode1,discountcode2])***

2) Find the code that the customer inputs and compare it to the code specified in step 1(this is the code I pasted above)

3) If 2 is a match perform discount code discussed further up in convo

4) End discount code

5) Output the result of the discount code

6) end the If statement

***this part is the part that I think could be improved upon, and why I don't want to specify exactly what I did out of a risk of filling the web up with bad code.

 

0 Likes
New Member
4 0 0

Fair enough a-aron, I totally get it. Thanks so much for your help though!

This is pretty much my 3rd day even looking at it - so while I get the concept I don't understand how to execute it in code.

If anyone else can help that'd be greatly appreciated! 

0 Likes