Prevent Discount Codes from Being Used on Certain Products

New Member
8 0 0

Yes

If a discount code "spend149" is in the user's cart, and the cart's total is $149 or more, the line items should be discounted 5%.

Only items that are tagged with the tag "coupon" get the 5% discount.

Here's the code block which achieves this:

#percentage discounts
if discount_percentage > 0
  
  Input.cart.line_items.each do |line_item|
    # if line_item.variant.product.tags.include? tag
      total_value += (line_item.line_price.cents/100)          
    # end
  end

  if total_value >= total_threshold
    
    cart = Input.cart
    cart.discount_code.reject(:message=>discount_message)
  
    cart.line_items.each do |line_item|
      if line_item.variant.product.tags.include? tag
      
        if discount_percentage > 0
          line_discount = line_item.line_price * (discount_percentage/100)
          new_price = line_item.line_price - line_discount
        end
    
        line_item.change_line_price(new_price, :message=>discount_message)
      end
    end
    

  end
end
 

 

0 Likes
Shopify Partner
215 0 11

Hello Everyone,

I was hoping to help a bit. Ive come up with an idea to apply discount only on items you want by using the cupon code as a flag to apply the discount the way you want it to apply. You just make a cupon of 0% and catch it on the script, if its the cupon your looking for then apply the discount to the products you want excluding the products you dont want. Its mainly the idea of how to do it more then the code. I know this might not be the way you all are looking for, but its a work around for now :P. HERE is the code I made. Ideas to make it better and cleaner are welcome. Im just starting on Ruby so dont be harsh haha. If you guys have any questions Ill be glad to help :D 

Cheers! 

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Tourist
11 0 1

Whats the status of of this one???

 

We are currently working on ways for the scripts to better interact with discount codes. You would be able to disable the code winthin the script if an item is on sale, for example. 

0 Likes
Shopify Expert
701 0 65

I just wanted to check in and confirm that this is a Cart-level rejection and that there is no way to reject discount codes at the line item level using Scripts?

I'm a million different people
0 Likes
Shopify Partner
215 0 11

@rickydazla Currently its not possible to make a discount cupon ignore just one line in the Cart. Though that would be awesome haha. But I did a work around for that which you can see HERE. Basically the idea is to make a 0% cupon and use it as a flag to make the discount yourself from the Scripts. For me it has worked really well. Good Luck! :D 

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
New Member
2 0 1

Don't know if this would help anyone but, I created two collections and hid them from the store. 1collection was for products that we allow discounts on. The second was for items that were not allowed discounts.  We then put all products in their respective collections and created a discount coupon for the specific collection. Since products can be in multiple collections it hasnt disrupted anything on our store and allows us to keep coupons from being applied to high cost, low margin items. 

0 Likes
New Member
2 0 0

Hi Andrew, am I right that I can place all items that I want to exclude to be discounted to one hidden collection and all the other to the second. Even if that items will be in other collections later, the coupon will not work for them?

0 Likes
New Member
2 0 1

As long as you are creating discount codes for the collections that contain only items you want discounted then you should be fine.  It doesn't matter if a discount item and a non discount item are in a separate similar collection. 

Example. I have a collection called "t-shirts" with discount and non-discount items. 

I create two new separate collections. One is called "discount shirts" the other is "non-discount shirts". I hide these two new collections from the store and create a discount code for "discount shirts" collection. I then put All shirts in their respective new collection.

Now only the the t-shirts in the "discount shirt" collection will allow the discount code to be applied. 

1 Like
New Member
1 0 0

I want to create a discount code that applies both to a customer in a group and a collection. Basically we have some bundles that we don't want to apply discounts to but we want to have a member discount that can be used on anything but the bundles. It doesn't look like there's a simple way to do this. Discount codes just don't have enough options. 

But it almost sound like I can make a cart level rejection that will reject using the code if a Bundle is in the cart. Is this correct? Can anyone give me some pointers to how this would work? 

I know this is completely wrong but here is my line of thought of what kind of code I'll need:

if discount_percentage does not equal nil

     if line_item.variant.product.tags.include? bundle

          cart.discount_code.reject(:message=>"No Bundles allowed with discount. Remove Bundled products and try again!")

0 Likes
Shopify Partner
215 0 11

Hey Michael, 

Ive made a Shopify Scritp which uses a cupon on 0% discount as a flag to apply the discounts on the products you decide with the code you would write. You can look a the examples HERE. For the information on the client you can use the object CUSTOMER from the input.cart.customer to find if he has the necesary tags to apply the discount. Its fairly easy if you know what you want :D If you need help just reply to this message, Ill gladly help you out :P Im sure you've seen the documentation HERE for the objects you can use in Shopify Scripts. Its really usefull. PLUS they've just included the Shipping object to offer free shipping or take into account the Shipping info. Good luck! :D 

Cheers!

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes