Having some problems with the Shopify js-buy-SDK.
I have been able to make a cart, and I have also tried fetching products, etc, and it works. But when I try to add a line item to the cart, the updated cart array, from addLineItems, returns empty. I know the variantId is correct, because if I change it I get an error.
Full code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://sdks.shopifycdn.com/js-buy-sdk/v1/latest/index.umd.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
const client = ShopifyBuy.buildClient({
domain: 'xxxx.myshopify.com',
storefrontAccessToken: 'xxxxxxx',
appId: '6'
});
// Create an empty checkout
client.checkout.create().then((checkout) => {
// Do something with the checkout
console.log(checkout.id);
x(checkout.id, client);
});
});
function x(check, client) {
const itemToAdd = [
{ variantId : 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzYyMDExMDQxMzg4NA==', quantity : 12 }
];
// Add an item to the checkout
client.checkout.addLineItems(check, itemToAdd).then((checkout) => {
console.log(checkout.lineItems); // THIS RETURNS AN EMPTY ARRAY
});
}
</script>
</head>
<body>
</body>
</html>
Are you using the productID or the variantID of the item you're trying to add?
In my experience, if an ID ends with ==
it's a productID.
Assuming the item you're trying to add only has one variant, try
const productId = 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0LzYyMDExMDQxMzg4NA==';
client.product.fetch(productId)
.then((product) => {
const variantId = product.variants[0].id;
console.log(variantId);
});
If you're seeing an ID-like string and not undefined
in your console then that is the correct value for your variantID.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.