New Shopify Certification now available: Liquid Storefronts for Theme Developers

Cart_Create, Cart_Update Webhook -> Cart Alteration

rich_mutt_ch
Shopify Partner
2 0 0

Hi I'm relatively new to Shopify development and I'm hoping that someone is able to help me stop going around in circles trying to achieve the following:

 

1.) My store is firing a JSON payload to my server-side app whenever cart/create or cart/update is triggered. 

2.) The standard JSON payload sent by Shopify contains an id and a token along with cart line_items

3.) Using either the id or token provided in the payload, I want to remove line items from the associated cart programmatically upon receipt of the webhook based on some conditionals. 

4.) It's critical that this is done server-side. 

 

I have explored:

 

1.) Using the storefront API. I can't find documentation confirming this for sure but it's my understanding that the id sent in the payload is not the same as the id that is used in the storefront API. This id comes from creating a cart via the storefront API directly which is outside of the scope of what I'm trying to do. 

 

2.) I have tried using the Admin REST API, which does not have direct cart methods but does have a modify checkout method which I believe would allow me to modify cart line items. I have found that this method requires Shopify app approval. Ideally I'd like to avoid going down this route completely by editing the cart without ever accessing the kind of sensitive information retrieved by checkout calls but I'm unable to see an alternative approach. 

 

3.) Could I call the Ajax endpoints i.e. example.myshopify.com/cart/clear.js? I could potentially attach the cart.js token to the cart/update cart/create webhook by setting custom attributes so that they available to my server-side app. This feels odd but I'm willing to try anything even if it's not conventional. 

 

Does anyone have any suggestions? Is there something obvious I'm missing? 

 

Thanks in advance to anyone able to assist!

 

 

 

 

 

Replies 2 (2)
Liam
Shopify Staff
Shopify Staff
1892 202 577

Hi Rich_mutt_ch,

 

Based on your requirements and the options you've explored, there are indeed a few ways to tackle this problem. Here's two suggestions I'd have:

  1. Using the Storefront API - The Storefront API provides a checkoutLineItemsReplace mutation that allows you to replace line items in a checkout. However, as you've pointed out, this might involve creating a checkout object first, which may not align with your use case. Also, as the Storefront API is typically used on the client-side, it may not be the best fit for your server-side requirements.

  2. Using the AJAX API - Shopify's AJAX API does indeed provide a /cart/clear endpoint that you can use to clear all items from the customer's cart. You can also use the /cart/change.js endpoint to the quantity of a specific line item in the cart. However, keep in mind that these endpoints are also typically used on the client-side. While it's technically possible to use these on the server-side, it might not be the most optimal solution and could lead to issues in future.

Given the above options, it seems using the AJAX API might be the most suitable solution for your needs, despite it not being the most conventional solution. You could set the cart token as a custom attribute in your webhook, then use this token in your server-side app to make requests to the /cart/clear.js or /cart/change.js endpoints.

 

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

rich_mutt_ch
Shopify Partner
2 0 0

Thank you for taking the time to respond Liam. 

 

I have been able to come up with a solution using the AJAX API. For the request to complete successfully outside of the immediate browser session, it seems to be a requirement that the cart token is sent as a cookie. This makes sense but wasn't immediately obvious to me. It also adds the complication of ferrying the cart cookie value to the server via the webhook but it is possible with some creative use of hidden line item properties. 

 

Thanks again for the guidance. 

Rich