Draft Order becoming a checkout

PLMServices
Tourist
6 1 1

Hi.

I'm just starting to look into Shopify development, so excuse my ignorance.

In short, I'm trying to write some code that will allow the applying of a whole-order discount to customers who are tagged appropriately when they are shopping through a standard Shopify web shop.
I imagine this could be written as a full-blown app, but I'm just looking to try this out by directly modifying a theme in my development store.

I've done some reading and people have suggested the appropriate way to do this is using the Draft Order API, and in fact the Shopify blog post announcing the Draft Orders API (https://www.shopify.com.au/partners/blog/introducing-shopify-s-draft-orders-api) and the API documentation itself (https://shopify.dev/docs/admin-api/rest/reference/orders/draftorder) both mention that applying discounts is a specific use case for this API.

So it was suggested elsewhere that you could use javascript to hijack the "Check Out" button from the Cart screen to call out to your own hosted code and create the draft order, and I can see the API calls for creating the draft order, modifying the item/order discounts and then converting/completing it to an actual order.

So my initial questions/confusions are:
1. How do I, from my hosted code, figure out what was in the customer's cart so I can build the draft order? I can see that there is a "cart" cookie in the request to the /cart resource, but I'm yet to find any documentation for a "cart" resource that would allow me to query for what is in the cart with that id. (Have I just missed it somewhere?)

2. How do I then get the newly-minted "Order" into the checkout. I can see that the normal process would be to POST to the /cart resource (still with the same cart cookie id) and that redirects to a /:shopid/checkouts/:checkoutid URL. But if I've gotten to the point where I've created the Draft Order, added the discounts and then completed it to an actual "Order", how do I leverage that into a checkout?

Am I just going at this wrong?

Any pointers to documentation or online example code would be most welcome.

Thanks.

0 Likes
PLMServices
Tourist
6 1 1

I can't edit my message, so by way of an update, I have found, in reference to my first question that there is a Cart API in the Ajax API, however that appears to only allow retrieval of the cart for the current session, so it's not the sort of thing that could be used to answer my #1 question I believe.

0 Likes
Shayne
Shopify Staff
Shopify Staff
202 9 46

Highjacking checkout like this is against the API TOS, it generally provides a pretty poor experience that doesn't account for any other apps / automation / shipping workflows that are built into the merchant's checkout.

If you're just looking to create a discount for a specific customer (or group of customers), you can use the Price Rule API, and have it automatically apply to your tagged customers.

Hope this helps!

Shayne | Developer @ 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

0 Likes
PLMServices
Tourist
6 1 1

Thanks for the reply Shayne.


@Shayne wrote:

Highjacking checkout like this is against the API TOS, it generally provides a pretty poor experience that doesn't account for any other apps / automation / shipping workflows that are built into the merchant's checkout.

Do you have a reference for this API TOS issue as that is super useful to know.

Also, the other discussions I'd seen on here (and elsewhere) all theorised that Apps that were trying to provide a "wholesale-like" pricing for VIP customers functionality basically worked the way I've described. And in fact the API documentation for the Draft Order API specifically mentions this use case in it's introductory text.


@Shayne wrote:

If you're just looking to create a discount for a specific customer (or group of customers), you can use the Price Rule API, and have it automatically apply to your tagged customers.


I've had a look at the Price Rule API you suggested and I can see how an appropriate Price Rule could be constructed but the documentation for the API seems to indicate that the utility of this API is to then assign a constructed Price Rule to a Discount Code, which isn't all that useful as in a Wholesale situation as you'd want the discount to be automatically applied to any qualifying customer without them having to enter a Discount Code.

Thoughts?

0 Likes
Shayne
Shopify Staff
Shopify Staff
202 9 46

The TOS is at https://www.shopify.com/legal/api-terms. On Jan 1st, the section about checkout will be updated to include: "Unauthorized use of an alternative to Shopify Checkout is not permitted for all apps."

You can't create a Price Rule that automatically discounts the catalog based on certain customers, and you can only have a single "automatic" Price Rule active at a single time — you will need to use discount codes in some capacity to offer discounts to the customer groups. If you had multiple automatic price rules, it would start getting difficult to manage the priority of these different price rules, since they can't stack. With discount codes, there's a specific selection of a Price Rule to apply to checkout.

If you want to automatically apply the discount at checkout for a specific customer group, you can use liquid in the theme to conditionally add:

?discount=discountcodehere

Then, the discount code will be applied at checkout for the specific customer.

Hope this helps!

Shayne | Developer @ 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

0 Likes