Shopify: custom scripts in the checkout to execute web requests

Viewed 24

I've a client that wants certain clients to buy with Store Credit. The amount of credit is stored in their external ERP system. My idea was to create a manual custom payment, show this payment method for users with a certain customer tag and add some scripts in the checkout in order to execute a web request and get how much credit the client has at that moment and do some validations (don't let them order if the checkout is bigger than the credit) in order to let them complete the order or not.

The only link I've found basically says: With a few exceptions, Shopify Scripts are not capable of input/output. This means that scripts can't execute web requests or database calls, and can't get input from a user. Source: https://help.shopify.com/en/manual/checkout-settings/script-editor/limitations#input-output

The client is aware they need to upgrade to Shopify Plus, but I can't find examples or any information that indicates this idea is doable.

1 Answers

I don't think Shopify Script is the solution in this case. (You can't make any outside call inside a script)

If you plan to use Shopify Plus you can modify checkout.liquid. You can create an app that has an endpoints that

  1. Tells you how many points the customer has
  2. Redeems X points and returns a discount code

Having that you can put a javascript that when checking out insert a button to redeem the points and if it's clicked you call the api and apply the discount.

To apply dynamically a coupon at checkout this is a working piece of code.


document.querySelector("#checkout_reduction_code").value = YOUR_CODE;
document.querySelector(".field__input-btn.btn").disabled = false;
document.querySelector(".field__input-btn.btn").click();

Other solution is to use Shopify Functions (https://shopify.dev/api/functions). This is a new feature that is availble in preview and lets you customize the checkout experience. I don't have experience with that but I suppose you can do something similar.

Related