Create a cart which is specific to the user

ChandaneshKC32
New Member
10 0 0

Hi All,
Currently am using below added code snippet to create cart with item and fetching the cart items which ever added but how to restrict to the user?
for an example if one user added items to the cart and other user can also able to see them now? How to restrict the other user not to fetch the same cart items which created by user(first)?

Add items to the cart method:

addtoCart = () => {
this.setState({ loading: 'true' });
var that = this;
var data = 'id=' + variantId + '&quantity=1'
//POST request 
fetch('https://lonelywalls.myshopify.com/cart/add.js', {
method: "POST",//Request Type 
body: data,//post body 
headers: {//Header Defination 
'Content-Type': 'application/x-www-form-urlencoded',
'x-shopify-access-token': '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',
},
}).then((response) => response.json())
.then((responseJson) => {
console.log("Cart Response", responseJson);
})
//If response is not in json then in error
.catch((error) => {
console.log(error);
this.setState({ loading: 'false' })
});
}

 

Fetch items from the created cart(method):

fetchCart = () => {
var data = [];
var that = this;
console.log('Fetching items from cart');
//GET request 
fetch('https://lonelywalls.myshopify.com/cart.js', {
method: "GET",//Request Type 
})
.then((response) => response.json())
//If response is in json then in success
.then((responseJson) => {
console.log("Cart Response", responseJson.items);
})
.catch((error) => {
console.log(error);
});
}

 

0 Likes
hashtd
Excursionist
15 1 3

dont use AJAX API

try `js-buy-sdk`

https://shopify.github.io/js-buy-sdk/

`client.checkout.create()` which allows to create multiple checkouts

then based on checkoutId you can add product to specific checkout

See => "Adding Line Items" for details

- Was my response helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
0 Likes