Limit Use of Discount code if Bulk discount is applied through Script Editor App

Shopify Partner
9 0 4

I Do Not want to offer more than 7% discount in my site at any point. 

I have used script editor app to create bulk discount, If customer buys more than 10 qty he will get 7% discount. I occasionally send coupon codes to customers who review for the product purchases for 5%. If that customer buys 10qty and uses 5 % discount he will get 7% + 5% =12%. (I want to avoid this)

Here is my current discount tiers. 

  1. 2 Qty > 2% discount + 5% coupon code = 7%
  2. 3 Qty >3% discount  + 5% coupon code = 8%
  3. 5 qty > 5% discount + 5% coupon code = 10%
  4. 10 qty> 7% discount + 5% coupon code = 12%

What i want

  1. 2 Qty > 2% discount + 5% coupon code = 7%
  2. 3 Qty >3% discount  + 5% coupon code cant be applied
  3. 5 qty > 5% discount + 5% coupon code cant be applied
  4. 10 qty> 7% discount + 5% coupon code cant be applied

Or Better

  1. 2 Qty > 2% discount + 5% coupon code = 7%
  2. 3 Qty >3% discount  + 5% coupon code applied but total discount is Max at 7%
  3. 5 qty > 5% discount + 5% coupon code applied but total discount is Max at 7%
  4. 10 qty> 7% discount + 5% coupon code applied but total discount is Max at 7%

Is there a way to achive this or some workaround. Some thing with "cart.discount_code.reject", I can copy and paste ruby code, but not write my own, please help. 

Ruby code that rejects any coupon code if qty is above 3 is what i need.

My current ruby script is 

DISCOUNTS_BY_QUANTITY = {
  2 => 2,
  3 => 3,
  5 => 5,
  10 => 7,
}

Input.cart.line_items.each do |line_item|
  next if line_item.variant.product.gift_card?

  quantity, discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
    line_item.quantity >= quantity
  end
  next unless discount

  message = "Bulk discount applied as quantity is #{quantity}."
  line_item.change_line_price(
    line_item.line_price * (Decimal.new(1) - discount.to_d / 100),
    message: message,
  )
end

Output.cart = Input.cart

 

 

0 Likes
Shopify Partner
132 1 16

Hi Pradeep,

I'm going to whip up some code for you. How are you planning on handling the potential for fixed amount discount codes? Should these just be added to the potential discount pool? e.g. a $15 discount from a $500 order is counted as a 3% discount originating from a code.

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
0 Likes
Shopify Partner
9 0 4

Hi,

I rarely (never) give away Fixed Amount discount codes. 

Basically I need to avoid customers apply discount codes if the quantity is more than 3.

It can also display message below the coupon code box saying "Coupon code not applied as Bulk Discount is added"

0 Likes
Shopify Partner
132 1 16

Hi Predeep,

I've written the code to max out the discount regardless of what % discount is applied. You can still set up your quantity discount breaks the same way as before, but now they won't only ever apply the minimum discount. Because you said you never give out fixed amount codes I've assumed that they will only apply to special cases and will be processed instead of the qty discount.

I've also changed the message output to be clear on what's happening when the discount is applied.

DISCOUNTS_BY_QUANTITY = {
  10 => 7,
  5 => 5,
  3 => 3,
  2 => 2
}

def AddPercentageDiscount(code_discount, max_discount_percent)
  #apply the capped percentage to each item
  Input.cart.line_items.each do |line_item|
    next if line_item.variant.product.gift_card?
  
    quantity, qty_discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
      line_item.quantity >= quantity
    end
    next unless qty_discount

    #combine discount and cap at max_discount_percent
    total_discount = qty_discount.to_d + code_discount.to_d
    total_discount = total_discount > max_discount_percent ? max_discount_percent : total_discount
    
    #determine % of discount originating from discount code
    if total_discount >= max_discount_percent
      capped_code_discount = total_discount.to_d - qty_discount.to_d
    else
      discount_iterations = ((qty_discount.to_d + code_discount.to_d) / max_discount_percent.to_d).floor
      capped_code_discount = (qty_discount.to_d + code_discount.to_d) - max_discount_percent.to_d * discount_iterations.to_d
    end

    #generate feedback message
    message = "#{qty_discount}% discount applied for bulk purchase of #{quantity} items."
    if code_discount > 0
      message += " #{capped_code_discount}% applied from discount code. Maximum of 7% total discount can be applied to this cart."          
    end
    
    #apply discount to line item
    line_item.change_line_price(
      line_item.line_price * (Decimal.new(1) - total_discount.to_d / 100),
      message: message,
    )
  end   
end

#calculate the dicsount as percentage if no 
case Input.cart.discount_code
when CartDiscount::Percentage
  AddPercentageDiscount(Input.cart.discount_code.percentage,7)
when CartDiscount::FixedAmount
  if Input.cart.subtotal_price >= Input.cart.discount_code.minimum_order_amount
    [Input.cart.subtotal_price - Input.cart.discount_code.amount, Money.zero].max
  else
    Input.cart.subtotal_price
  end
else
  AddPercentageDiscount(0,7) 
end

Output.cart = Input.cart

If you need further help you can ping me on my account email and I'll let you know what's involved.

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
0 Likes
Shopify Partner
9 0 4

Hi,

Thank you so much, you understood my requirement and wrote the code exactly for what I needed. 

The code is running perfectly in the Backend(in the script editor app). However in the site. It is not working. Please check the following link. https://www.tanotis.com/10341611/checkouts/a7e0f39788705327b7109b1f1a6f271a?_ga=2.242336390.52475941...

The example cart has 7% discount for bulk items and 5% discount for coupon code POCKET05

Discount coupon is getting added as before.

 

 

0 Likes
Shopify Partner
132 1 16

Hi Pradeep,

Apologies, I forgot to include the strip for the original discount. The following will apply the discount per line item and then remove the discount from the total cart.

DISCOUNTS_BY_QUANTITY = {
  10 => 7,
  5 => 5,
  3 => 3,
  2 => 2
}

def AddPercentageDiscount(code_discount, max_discount_percent)
  #apply the capped percentage to each item
  Input.cart.line_items.each do |line_item|
    next if line_item.variant.product.gift_card?
  
    quantity, qty_discount = DISCOUNTS_BY_QUANTITY.find do |quantity, _|
      line_item.quantity >= quantity
    end
    next unless qty_discount

    #combine discount and cap at max_discount_percent
    total_discount = qty_discount.to_d + code_discount.to_d
    total_discount = total_discount > max_discount_percent ? max_discount_percent : total_discount
    
    #determine % of discount originating from discount code
    if total_discount >= max_discount_percent
      capped_code_discount = total_discount.to_d - qty_discount.to_d
    else
      discount_iterations = ((qty_discount.to_d + code_discount.to_d) / max_discount_percent.to_d).floor
      capped_code_discount = (qty_discount.to_d + code_discount.to_d) - max_discount_percent.to_d * discount_iterations.to_d
    end

    #generate feedback message
    message = "#{qty_discount}% discount applied for bulk purchase of #{quantity} items."
    if code_discount > 0
      message += " #{capped_code_discount}% applied from discount code. Maximum of 7% total discount can be applied to this cart."          
    end
    
    #apply discount to line item
    line_item.change_line_price(
      line_item.line_price * (Decimal.new(1) - capped_code_discount.to_d / 100),
      message: message,
    )
  end   
end

#calculate the dicsount as percentage if no 
case Input.cart.discount_code
when CartDiscount::Percentage
  AddPercentageDiscount(Input.cart.discount_code.percentage,7)
  Input.cart.discount_code.reject({ message: 'Your discount has been applied to each line item' })
when CartDiscount::FixedAmount
  if Input.cart.subtotal_price >= Input.cart.discount_code.minimum_order_amount
    [Input.cart.subtotal_price - Input.cart.discount_code.amount, Money.zero].max
  else
    Input.cart.subtotal_price
  end
else
  AddPercentageDiscount(0,7) 
end

Output.cart = Input.cart

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
0 Likes
Shopify Partner
9 0 4

Hi,

Thank you for your time. Still the problem is not solved. following are the issues with new code.

  1. Now it is showing the message . "Your discount has been applied to each line item", as soon as we enter the checkout page. Even though coupon is entered or not.
  2. Bulk Discount is not being applied if qty is more than 10
  3. I am not able to enter coupon code even if qty is 1. Coupon code field is totally useless as it always shows message "Your discount has been applied to each line item"

May be there is some small tweak which is required.

 

0 Likes
Shopify Partner
132 1 16

Hi Pradeep,

I've altered the script to work from the discount code first so that there is no longer any need for the code to be rejected. There will no longer be any feedback from the code entry section in the cart, feedback will only be available on the line items.

DISCOUNTS_BY_QUANTITY = {
  10 => 7,
  5 => 5,
  3 => 3,
  2 => 2
}

MAX_DISCOUNT_PERCENTAGE = 7

def AddPercentageDiscount(code_discount, max_discount_percent)
  #apply the capped percentage to each item
  Input.cart.line_items.each do |line_item|
    next if line_item.variant.product.gift_card?
  
    quantity, qty_discount = DISCOUNTS_BY_QUANTITY.find do |qty_break, disc|
      line_item.quantity >= qty_break
    end
    next unless qty_discount

    #cap total discount at max_discount_percent
    remaining_discount = max_discount_percent.to_d - code_discount.to_d
    remaining_discount = remaining_discount > 0 ? remaining_discount : 0
    
    if remaining_discount > qty_discount
      remaining_qty_discount = qty_discount
    else
      remaining_qty_discount = remaining_discount
    end
    
    #generate feedback message
    message = "#{remaining_qty_discount}% discount applied for bulk purchase of #{quantity} items."
    if code_discount > 0
      message += " #{code_discount}% applied from discount code."          
    end
    message += " Maximum of 7% total discount can be applied to this cart."

    #apply discount to line item
    if remaining_discount > 0
      line_item.change_line_price(
        line_item.line_price * (Decimal.new(1) - remaining_discount.to_d / 100),
        message: message,
      )
    end   
  end
end

#calculate the dicsount as percentage if no 
case Input.cart.discount_code
when CartDiscount::Percentage
  AddPercentageDiscount(Input.cart.discount_code.percentage,MAX_DISCOUNT_PERCENTAGE)
when CartDiscount::FixedAmount
  if Input.cart.subtotal_price >= Input.cart.discount_code.minimum_order_amount
    [Input.cart.subtotal_price - Input.cart.discount_code.amount, Money.zero].max
  else
    Input.cart.subtotal_price
  end
else
  AddPercentageDiscount(0,MAX_DISCOUNT_PERCENTAGE) 
end

Output.cart = Input.cart

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
0 Likes
Shopify Partner
9 0 4

 @Mandelbrotian

 

Sorry for bothering you after a long time. The Code was not being used since it had a bug. Somehow the code is not working as intended. 

It is displaying the messages properly but always calculating discount@7% irrespective of the quantity. 

 

I can help you with additional data if you need them.

 

0 Likes
Highlighted
New Member
1 0 0

Hi Mandel, 

 

We're having a tough time with this and this thread was forwarded to us by support as we may need some custom code to solve tour problem.

 

Right now we have volume discounts (an app) that applies a per piece discount when customers buy over x units in a certain collection. Right now the collections are 12g, 25g, 30g, 50g, 80g, and 120g jerky. The problem we are facing is that when the bulk discount is applied it automatically takes away the option to add further discounts. 

 

I was wondering since this is talking about bulk discounts if there would be a way that by implementing code we could have the volume discounts applied and also keep the option to further add a discount code at checkout. 

 

Cheers,

 

Big Chief Team

0 Likes