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
10035 119 1871

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

★ Winning Partner of the Build a Business competition. ★ 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.