Ability to apply a Discount Code to the cart via a script

Massimo_Andreas
New Member
1 0 0

Is it possible to do something like this?

if Input.cart.discount_code.nil?
  Input.cart.apply_discount_code("code")
end

Output.cart = Input.cart
 

If not, it would make life a lot easier.

0 Likes
Jason
Shopify Expert
10360 158 2008

No. Shopify Scripts can not add Discount Codes to the cart - only reject them.

I jump on these forums to help and share some insights. Not looking to be hired, and not looking for work.

Don't hand out staff invites or give admin password to forum members unless absolutely needed. In most cases the help you need can be handled without that.


★ http://freakdesign.com.au ★
0 Likes
Rasmus_Larsen
Shopify Expert
1 0 1
<script>
  var countries = ["BE", "BG", "CZ", "DK", "DE", "EE", "IE", "GR", "ES", "FR", "HR", "IT", "CY", "LV", "LT", "LU", "HU", "MT", "NL", "AT", "PL", "PT", "RO", "SI", "SK", "FI", "SE", "GB", "IS"]
  $.get("https://ipinfo.io", function(response) {
    if(countries.indexOf(response.country) === -1) {
      var discount = '<input type="hidden" name="discount" value="EU tax regulation">'
      $('form').append(discount);
    }
  }, "jsonp");
</script>

Hi,

I was able to add discount code on the cart via script, dont know if this help you.

This will add a discount code to the cart if the user is our case is outside EU.