Access a community of over 900,000 Shopify Merchants and Partners and engage in meaningful conversations with your peers.
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);
});
}
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
User | RANK |
---|---|
3 | |
2 | |
1 | |
1 | |
1 |