Reject a discount code in a SHIPPING script

Highlighted
Excursionist
40 0 9

Has anybody had sucess with rejecting a discount code in a Shipping script?

It works with line item scripts.. but with shipping scripts I keep on getting a "method not found" error:
[Error] undefined method 'reject' for #<CartDiscount::Shipping:0x7fe77148dc30>

Thanks!

0 Likes
Highlighted
Shopify Partner
20 0 10

Crane and Canopy -

Can you provide a real-life example of how and why you want to reject a discount code in a shipping script?

Thanks!
James

0 Likes
Highlighted
Excursionist
40 0 9

Free Ground shipping on orders over $150, using discount code FREESHIP.

No way to do this with discount codes.  Shipping scripts works (almost):

Set a discount code FREESHIP to 0% off

If discount code  = FREE SHIP

    if cart > 150

       set ground shipping cost to $0

    else

      reject discount code and send error

   end

end

0 Likes
Highlighted
Shopify Partner
20 0 10

Crane and Canopy -

OK. Here's what I have... let me know if it works for you.

I've created a Discount Code = FREESHIP that is 0% off if order >= $150. I have shipping rate(s) with "ground" in the name. Here is the shipping script:

# Is there a discount code?
unless Input.cart.discount_code.nil?
  Input.shipping_rates.each do |shipping_rate|
    next unless shipping_rate.name.downcase.include? "ground"
    
    # If the shipping rate name includes "ground" AND subtotal > $150 then set free shipping
    if Input.cart.subtotal_price > Money.new(cents: 100) * 150
      shipping_rate.apply_discount(shipping_rate.price * 1.00, message: "Free ground shipping with FREESHIP")
    end
  end
end

Output.shipping_rates = Input.shipping_rates

The customer can't use the Discount Code unless the order subtotal is > $150. If the order subtotal > $150 AND the customer enters Discount Code = FREESHIP, then the script will make "ground" shipping rates free (100% discount).

Hope that helps. Let me know if you have questions.

James

 

0 Likes
Highlighted
Excursionist
40 0 9

James -

Thanks for the help.  I acutally already have very similar code written up.  Just wanted to add an error message and remove the discount if the cart was below $150 (and the code was used).  My current solution is to just change the name of the Ground Shipping rate to "Ground Shipping - increase your cart for free shipping".  Not ideal, but works.  Would be better if i could use the discount.reject method.... but no go cause this is a shipping script.

THanks again for the help though!

Chris

0 Likes