Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Create cart event not firing webhook in development store

Solved

Create cart event not firing webhook in development store

provenq
Shopify Partner
22 0 3

Hi,

 

Recent app developer n00b:

 

I created a development store, and am able to successfully get my app installed.  During installation, I use the Shopify node SDK to create a webhook on the development store subscribing to the carts/create topic.  When I run a query against the shop's (in Postman) admin/api/2019-04/webhooks.json endpoint, I see the registered webhook is there.

 

I also make a call against the webhook I have registered, and it successfully runs and returns a 200 status (in Postman).

 

However, when I go to the online site for the development store, and click to Add to Cart for the sample product, it adds the product to a cart, but the registered webhook is never fired.  Even when I run the events API call, admin/api/2019-04/events.json, I don't see the create/cart event there at all (just events related to creating the homepage, etc).

 

So, do development stores fire webhooks?  Is adding a product to a cart going to generate a create/cart event?  Why doesn't this event show up in the list of events?

 

Banging my head against the wall, so I'd appreciate any help on this.  The endpoint registered for the webhook is an https endpoint (AWS lambda).

Accepted Solution (1)
Alex
Shopify Staff
1561 81 343

This is an accepted solution.

Hey @provenq.

 

Sorry for the late reply! My team was pretty busy in the lead up to Unite, since then I've amassed time to catch up on some loose ends, starting with this.

 

I think I know what's happening here. Clearing a cart does not destroy the cart you initially created. You can see this by first adding an item to the cart on the storefront, and inspecting your cookies. There should be a cookie under the storefront domain with the key `cookie` and a value representing a unique ID of the cart. If you then remove the line item from the cart and/or add new items to the cart after, the cookie with that exact value remains. In my tests, this worked in a way that I expected. The webhook fires initially, but not after clearing the cart and adding new products after. I was able to test this by simply deleting the cart cookie before each add-to-cart to fire a new webhook.

 

The same applies to a checkout for the most part. The cart creation seems to hinge on a cookie with the key `checkout`, but I'm not 100% certain on that. I can view and access the cookie on the checkout which shares the same domain as the storefront, but I can't see it from the cart page. I'm sure there's more to it than that, but deleting that cookie while on the checkout, navigating back to the cart, then clicking checkout from the cart page effectively creates a new checkout, firing a new webhook.

 

Are you still having issues with webhook delivery with the above considered? Let me know if so. Hope that helps!

 

Cheers.

Alex | 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 the Shopify Help Center or the Shopify Blog

View solution in original post

Replies 7 (7)

Alex
Shopify Staff
1561 81 343

Hey @provenq.

 

Can you provide the ID of the webhook you created? I can use that to check logs on my end.

 

Cheers.

Alex | 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 the Shopify Help Center or the Shopify Blog

provenq
Shopify Partner
22 0 3

Thanks for the response (I missed it as I was on a short vacation).

 

The webhook ID is 442056835127 for the carts/create webhook.

The webhook ID is 442056867895 for the checkouts/create webhook.

 

I didn't see any calls on our side when trying to checkout a product on our dev store.  Could you explain when either of these events should fire?  I thought that clicking to add a product to cart would fire the carts/create, and that clicking Checkout in the dev store cart would trigger a checkouts/create, but neither seemed to call our webhook.

 

It's odd, though.  I went to test it again, just now, and it seemed to generate a call (I saw a log entry).  Then I cleared the cart, and tried it one more time, and no calls seemed to be made.  Then I cleared the cart again, added a product to the cart, then clicked checkout, and still no calls to the webhook handler on our side (I see no log entries).  I just tried adding a product to a cart, checking out, then clearing the cart about 5 times and no calls to the webhook seemed to be generated.

 

Also, I thought I maybe wasn't registering the correct events.  So I tried adding another, checkouts/paid, but I got a 404 when calling the code below to register the webhook (node shopify sdk, BASE_URL is the url to our domain):

webhookresult = await shopify.webhook.create({
address: `${BASE_URL}/v1/webhooks`,
topic: 'checkouts/paid',
format: 'json'
});
Alex
Shopify Staff
1561 81 343

This is an accepted solution.

Hey @provenq.

 

Sorry for the late reply! My team was pretty busy in the lead up to Unite, since then I've amassed time to catch up on some loose ends, starting with this.

 

I think I know what's happening here. Clearing a cart does not destroy the cart you initially created. You can see this by first adding an item to the cart on the storefront, and inspecting your cookies. There should be a cookie under the storefront domain with the key `cookie` and a value representing a unique ID of the cart. If you then remove the line item from the cart and/or add new items to the cart after, the cookie with that exact value remains. In my tests, this worked in a way that I expected. The webhook fires initially, but not after clearing the cart and adding new products after. I was able to test this by simply deleting the cart cookie before each add-to-cart to fire a new webhook.

 

The same applies to a checkout for the most part. The cart creation seems to hinge on a cookie with the key `checkout`, but I'm not 100% certain on that. I can view and access the cookie on the checkout which shares the same domain as the storefront, but I can't see it from the cart page. I'm sure there's more to it than that, but deleting that cookie while on the checkout, navigating back to the cart, then clicking checkout from the cart page effectively creates a new checkout, firing a new webhook.

 

Are you still having issues with webhook delivery with the above considered? Let me know if so. Hope that helps!

 

Cheers.

Alex | 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 the Shopify Help Center or the Shopify Blog

provenq
Shopify Partner
22 0 3

Wow, that is interesting.  That might explain why it seemed random.  I will try clearing my cookies before attempting next time.  I will try that sometime today if I can.  But yeah, even given the other stuff folks mentioned, I was still not seeing the webhook fire, or if it did, it seemed to be delayed, though that might be explained by the cookies, since it may have expired by the next time I tried and wasn't thinking about the webhook at the time.

 

I'll definitely give this a try, thanks for the info. 

provenq
Shopify Partner
22 0 3

Yeah, the cookie thing was it. I didn't know that would have an effect, and explains the delay I would see.  I would test and try and get cart webhook events to show up on a Friday, and would get frustrated, and not try it again until many days later, after which the cookie would have expired, and then the webhook would fire (or someone else on the team did it as well, and our cookies expired at different times).

 

I think we might wanna see that in the docs somewhere in the webhook section on testing the webhooks.  I did waste many hours thinking my code was broken cause I wasn't seeing the webhooks get called on our side.

 

Thanks for the response and the explanation!

abdul81
Shopify Partner
20 1 3

@provenq I'm creating a shopify app with nodejs and express and when I try to subscribe to carts/create event I get the following error body: 

 

 

{
    "errors": {
        "topic": [
            "Invalid topic specified: carts/create. Does it exist? Is there a missing access scope? Topics allowed: app/uninstalled, collections/create, collections/delete, collections/update, products/create, products/delete, products/update, shop/update, app_purchases_one_time/update, app_subscriptions/update, domains/create, domains/update, domains/destroy\n"
        ]
    }
}

 

 
I am curious what access scopes you have provided during app installation? Will be great if you can help with that
 
Regards 

 

Product Manager at Developer's Capital

  • Creator of App Quantity & Volume Discounts
  • Was my reply helpful? Click Like to let me know!
  • Was your question answered? Mark it as an Accepted Solution
,new value=Product Manager at Developer's Capital
  • Creator of App Quantity & Volume Discounts
  • Was my reply helpful? Click Like to let me know!
  • Was your question answered? Mark it as an Accepted Solution
pallavi57
Shopify Partner
5 0 2

Ya I tried And it worked but how do you expect this to happen in production. I mean we cant clear cookies every time. There has to be some workaround on this