Solved

JS Buy SDK - Discount Codes not responding to maximum discount uses

SimplyCoffee
Visitor
3 0 0

Hi All,

 

We run a medium sized ecommerce website on a custom website integrating with shopify using the js-buy-sdk. One thing we have discovered (and so have some of our customers). Is that our discount codes aren't locked down. Locked down meaning they aren't responding to the settings in Maximum Discount Uses (Limit number of times this discount can be used in total. And Limit to one use per customer. 

 

Our discount implementation is very similar to the demo in the read me https://github.com/Shopify/js-buy-sdk#adding-a-discount

 

 

const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8'; // ID of an existing checkout
const discountCode = 'best-discount-ever';

// Add a discount code to the checkout
client.checkout.addDiscount(checkoutId, discountCode).then(checkout => {
  // Do something with the updated checkout
  console.log(checkout);
});

 

 

If you have any suggestions as to how I can make the checkout respect this discount rules. Please let me know

Accepted Solution (1)

Liam
Shopify Staff
2731 298 769

This is an accepted solution.

Hi SimplyCoffee,

 

While the JavaScript Buy SDK can be used to interact with the Shopify API and apply discounts to the checkout, the SDK itself doesn't enforce max discount uses rules. The enforcement of discount code limits and restrictions should be implemented on the server-side. In order to manage the limits you create on the admin, you'll need to implement additional logic in your backend or Shopify store.

 

When a customer applies a discount code, you need to validate it on your backend before making changes to the checkout. The general process would be to retrieve the discount code details from the Shopify API and check if it has exceeded the Maximum Discount Uses or if it has already been used by the customer. If the code is no longer valid based on these settings, you can reject the discount application and display an appropriate message to the customer.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

View solution in original post

Reply 1 (1)

Liam
Shopify Staff
2731 298 769

This is an accepted solution.

Hi SimplyCoffee,

 

While the JavaScript Buy SDK can be used to interact with the Shopify API and apply discounts to the checkout, the SDK itself doesn't enforce max discount uses rules. The enforcement of discount code limits and restrictions should be implemented on the server-side. In order to manage the limits you create on the admin, you'll need to implement additional logic in your backend or Shopify store.

 

When a customer applies a discount code, you need to validate it on your backend before making changes to the checkout. The general process would be to retrieve the discount code details from the Shopify API and check if it has exceeded the Maximum Discount Uses or if it has already been used by the customer. If the code is no longer valid based on these settings, you can reject the discount application and display an appropriate message to the customer.

 

Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog