TimeQuotaExceeded shopify script error Production Error

Shreyank
Excursionist
12 1 2

Hello,

     I have been running a script to discount all vip members giving a discount and the script was published in the June and till now it was running fine and suddenly yesterday I got an production error saying 

TimeQuotaExceeded  - Message: Your script exceeded the time limit.
Checked the code and couldn't find any errors in the script and script seems to work fine with the more products as well. The order on which it failed seems to have just one cart item. Cant find an error and would like to know what is causing this error and how to overcome it ? I am pasting the code for the reference and appreciate your time and efforts for looking into this.
 
 

 

# If the customer is a VIP member, give them a discount on all items
DISCOUNT_PERCENT = 0
DISCOUNT_MESSAGE = ''

if !Input.cart.customer.nil? 
  Input.cart.customer.tags.each do |tag|
    if tag.start_with?('Discount: ')
      DISCOUNT_PERCENT = (tag.split('%').first).split(' ').last.to_i / 100
      DISCOUNT_MESSAGE = tag.split(': ').last + ' OFF'
    end
  end
end
 

if DISCOUNT_PERCENT > 0 
  
  if Input.cart.discount_code
    Input.cart.discount_code.reject(
      message: "Discounts can not be used with your everyday discount."
  )
  end
  
  Input.cart.line_items.each do |line_item|
    
    if !line_item.variant.compare_at_price.nil? 
      if (line_item.variant.compare_at_price * line_item.quantity * (1-DISCOUNT_PERCENT)) <= line_item.line_price
        line_item.change_line_price(line_item.variant.compare_at_price * line_item.quantity * (1-DISCOUNT_PERCENT),message: DISCOUNT_MESSAGE)
        new_properties = line_item.properties
        new_properties.merge!({"_discount_note" => DISCOUNT_MESSAGE})
        line_item.change_properties(new_properties, message: "")
      else
        new_properties = line_item.properties
        new_properties.merge!({"_discount_note" => "Sale price less than discount"})
        line_item.change_properties(new_properties, message: "")
    end
    else
      line_item.change_line_price(line_item.line_price * (1-DISCOUNT_PERCENT), message: DISCOUNT_MESSAGE)
      new_properties = line_item.properties
      new_properties.merge!({"_discount_note" => DISCOUNT_MESSAGE})
      line_item.change_properties(new_properties, message: "")
    end
  end
end

Output.cart = Input.cart

 

 
Let me know of any possible solutions and the way to handle it.
Thanks
0 Likes
Jason
Shopify Expert
10037 119 1873

You have code in place that is nth scaling. One you have something that loops over customer tags, and the other that loops over all the items in the cart.So I could see how a customer will lots or tags - or a larger cart - could push you closer to the existing limits.

  • Do you need those loops?
  • Have you tested your code on larger carts, or with customers with many tags?

I would like to see you use two tags since it looks like you're using a tag to drive a discount. A customer (that knows how) could add tags to their own account so you're opening yourself to some risk here. A second tag would give you added security, and make it harder for someone to guess what it is. Would strongly recommended you don't post the shop url here if you are not going to change that.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes