Fetching cart contents on an external domain

shopdev2
New Member
1 0 0

Up until Chrome 80's samesite cookie changes, we were fetching Shopify's cart contents using:

 

$.ajax({
        dataType:'jsonp',
        url: '//<shop>.myshopify.com/cart.json',
        success: function(data){
          var response = $.post(
            '/' + drupalSettings.path.currentLanguage + '/cart',
            data,
            function (response) {
              <snip>
            }
          );
        }
      });

With chome's samesite cookie change, this no longer works as the session is not passed/used. Is there any alternative best practice to load cart contents on an external domain?

Replies 7 (7)
ScreenStaring
Shopify Partner
53 2 10

Hi, 

 

With chome's samesite cookie change, this no longer works as the session is not passed/used.

What makes you think this is the cause? The AJAX API does not require credentials so cookies being sent should not matter and using JSONP avoids CORS restrictions. Can you provide more info?

ScreenStaring
Software Development & Consulting
rlaa
Tourist
7 0 3

Hello,

We are also facing similar issue (cannot fetch cart contents via jsonp-requests).
I also assume this is due to the chrome's samesite cookie change (this started working after I disabled the samesite cookie changes in chrome settings).

I guess it happens because the jsonp-request cannot read this cookie (which contains the cart token):
cart_token.png
and because the jsonp-request cannot read this cookie, the request will always return new (empty) cart instead of the customers 'actual' cart.

We also looking for any solution to overcome this issue. Currently, the only feasible one seems to be that we should populate the data to our custom checkout application via POST request (submitting the form in the cart-page) but this won't still solve all the issues (e.g clearing the users cart).

Kind regards,

lenafuchs
New Member
8 0 0

hi! were you able to solve this problem? 

lenafuchs
New Member
8 0 0

Hi! Were you able to come up with a solution for that problem? I'm currently facing the same one

rlaa
Tourist
7 0 3

We were unable to fetch cart contents in front-end via jsonp-requests.

 

Instead, we opted to use POST request (form submit) from the store domain to our external domain.

 

Clearing the cart was again solved with:

1. Redirecting user back to the store domain and 

2. Clearing the cart with JS

3. Redirect back to the external domain

lenafuchs
New Member
8 0 0

Could you give me a little bit more detail on how you used the post request to get the cart data?

rlaa
Tourist
7 0 3

Sure,

 

On the cart page (in your shopify theme) you should already have an existing form-tag, e.g.

 

 

<form action="/checkout">
...
</form>

 

 

 This form by default redirects you to the Shopify's own checkout page.

 

 

 

You can however change the target (action) url for the form, e.g.

<form action="https://your-domain.com/some-endpoint">
...
</form>

 Now, you also have to have server side code which handles this form submit, like described here: 
https://developer.mozilla.org/en-US/docs/Learn/Forms/Sending_and_retrieving_form_data#on_the_server_...