How to get current cart

How to get current cart

Marconino
Shopify Partner
6 0 2

Hi ! 
If my customer had already a cart with products, how can I get this cart for add other product with my custom app ?
In the Storefront API, I seen cart query but with an id (that I don't know) 😕

Kind regards

Replies 12 (12)

SBD_
Shopify Staff
1831 273 419

Hey @Marconino 

 

Your app would need to read the Cart ID - one approach would be to use the AJAX API. Keep in mind cart interoperability is only just starting to roll out (share a cart with Liquid/Storefront API) and wont be available on all stores yet, so it's safest to also update the cart using the AJAX API too.

Scott | Developer Advocate @ Shopify 

Marconino
Shopify Partner
6 0 2

Hi, thank you for the reply.

 

I've seen that you can use AJAX API like this : https://shopify.dev/docs/api/ajax#making-requests-to-the-api

 

But I'm using a Unity webGL application that makes HTTP requests (UnityWebRequest)

 

Is it possible to use HTTP requests with the AJAX API?

(Sorry if this is a dumb question, I'm a beginner)

SBD_
Shopify Staff
1831 273 419

Hey @Marconino,

 

I'm not familiar with Unity webGL, but you'll need the session/cookie to access the cart via AJAX - so I suspect Storefront API will be your best bet!

Scott | Developer Advocate @ Shopify 

Marconino
Shopify Partner
6 0 2

But it is possible to get current cart of my customer with Storefront API ? 😕
I don't understand how 😔

For more context, I have php script which execute a request to Shopify and I want to get current cart of my customer

scerelli
Shopify Partner
4 0 1

Hello @Marconino did you find a way to get the current user cart? i'm facing the same problem.

Marconino
Shopify Partner
6 0 2

Hello @scerelli ! I use this code in Javascript for add product to current user cart ! 🙂
(This request is on one of my Shopify pages, if you want to add product from custom app, you have to create a proxy between Shopify and your app)

function addToCart(variantId, quantity) {

let data = {
'items': [{
'id': variantId,
'quantity':quantity
}]
};
fetch('/cart/add.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
}

scerelli
Shopify Partner
4 0 1

Thanks, it’s the same I’m using. I wanted to find a way to add to cart from the backend as there are important calculations and price cart transformations to be done before adding products to cart, if using my app, but so far I could find a way, only frontend side.

Marconino
Shopify Partner
6 0 2

You should to check about Shopify proxy : https://shopify.dev/docs/apps/online-store/app-proxies
In "Proxy URL" you put your url backend like this https://siteweb/add_product_to_cart.php for example, and in this script you use addToCart function !

scerelli
Shopify Partner
4 0 1

I will check it out! thanks

ajangir
New Member
6 0 0

is there any API which gets the cart information using the customer token for the user

 

SakthiKumar
Shopify Partner
1 0 0

I'm also facing the same issue, need guidance to get the API which gets the cart information using the customer token for the user, can anyone help me Out 

UmarAhmed
Shopify Partner
7 0 1

Did anyone get any solution?