discount code .start_with?

Highlighted
New Member
28 0 0

Hey there. I am trying to use the start_with method in the below shipping script, to apply the discount if the discount code in the cart starts with "2D". However, I'm getting an undefined method error.  Any ideas? Is my syntax wrong?

 

MINIMUM_ORDER_AMOUNT = 1 #dollars required in cart to get discount
DISCOUNT = 1 #percentage discount (in decimal format)
MESSAGE = "" #promotional message
code = Input.cart.discount_code
if Input.cart.subtotal_price_was > (Money.new(cents:1) * MINIMUM_ORDER_AMOUNT)
  Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.name == "First Class Package" && code.start_with?('2D')
    shipping_rate.apply_discount(shipping_rate.price * DISCOUNT, message: MESSAGE)
  end
end

Output.shipping_rates = Input.shipping_rates

 

0 Likes
Highlighted
New Member
28 0 0

Bump

0 Likes
Highlighted
Shopify Partner
132 1 18

Hi Nikolas,

Your actually trying to .start_with the discount object. If you use the following you'll isolate the code string.

MINIMUM_ORDER_AMOUNT = 1 #dollars required in cart to get discount
DISCOUNT = 1 #percentage discount (in decimal format)
MESSAGE = "" #promotional message
discount = Input.cart.discount_code
code = discount.code
if Input.cart.subtotal_price_was > (Money.new(cents:1) * MINIMUM_ORDER_AMOUNT)
  Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.name == "First Class Package" && code.start_with?('2D')
    shipping_rate.apply_discount(shipping_rate.price * DISCOUNT, message: MESSAGE)
  end
end

Output.shipping_rates = Input.shipping_rates

 

For reference: https://help.shopify.com/en/manual/apps/apps-by-shopify/script-editor/shopify-scripts

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
1 Like
Highlighted
New Member
28 0 0

Thanks Eliot. I tried your solution and I am getting an undefined method error on line 5. (code = discount.code)

0 Likes
Highlighted
Shopify Partner
132 1 18

Apologies, I didn't account for a no discount code situation

MINIMUM_ORDER_AMOUNT = 1 #dollars required in cart to get discount
DISCOUNT = 1 #percentage discount (in decimal format)
MESSAGE = "" #promotional message
code = ''
if Input.cart.discount_code
  code = Input.cart.discount_code.code
end
if Input.cart.subtotal_price_was > (Money.new(cents:1) * MINIMUM_ORDER_AMOUNT)
  Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.name == "First Class Package" && code.start_with?('2D')
    shipping_rate.apply_discount(shipping_rate.price * DISCOUNT, message: MESSAGE)
  end
end

Output.shipping_rates = Input.shipping_rates

That should do the trick.

Cheers,

Elliott

Feeling a bit lost? Contact elliot@mandelbrotian.com for help with theme setup, alterations, custom functionality, and app development.
1 Like
Highlighted
New Member
28 0 0

Thanks! that worked. I was trying to figure out for the longest time how to deal with 'code' when the discount code returned nil. 

0 Likes