Script editor - Only apply discount to non sale items

Hi,

I have a code for site members to get a 5% discount.

This discount should only be applied to line items that are at full retail price.

If a product is being sold at a discount already, it should not have this code’s discount applied to it as well.

I thought about checking each line item in the cart for that line items Price and Compare At price values, but I can’t find a reference for how to do this in the script editor?

Perhaps there is a better way?

Can someone help me figure this out please?

I decided to change the logic just a little to more easily solve this problem.

Instead of requiring the use of a discount code, I switched it to checking a customers tags for a specific tag, and then applying the cart line item logic to that instead - so now if the customer has the correct tag, I check each line item in the cart for an empty compare_at_price field, and apply the discount to that line item automatically when that field is empty.

I used the sample script for discount by customer tag, and modified the cart.line_items.each section to incorporate checking for items already discounted.

This is the modification to the default script that worked for me… in case it is of use to someone else.

cart.line_items.each do |line_item|
  if line_item.variant.compare_at_price.nil?
    next unless product_selector.match?(line_item)
    discount_applicator.apply(line_item)
  end
end

Cheers,

Rob