我正在尝试找出如何调整多个购物车项目。
本质上,我们有一个自定义订单页面,可将多个产品添加到购物车。添加的所有产品都具有相同的独特属性。
例如将这两款产品添加到购物车:
Product 1
ID: 1000
Property: CustomProduct2
Product2
ID: 1001
Property: CustomProduct2
最终用户仅将其视为一个产品,因此我希望有一种方法可以通过一个按钮删除或调整具有匹配属性的所有产品的数量。
我知道下面的方法行不通,但假设如果可能的话,它会是这样的:
$(document).on('click','.remove',function(e){
var property = $(this).attr('data-property');
$.ajax({
type: 'POST',
url: '/cart/add.js',
data: {
quantity: 0,
id: *,
properties: {
'Property': data-property
}
},
dataType: 'json',
async:false,
});
});
这可以通过使用
/cart/update.js
端点来实现。 (参见Shopify官方文档
文档忽略的一点是,您可以使用变体 ID 或行项目的“键”值作为负载的键。这在使用行项目属性时非常重要,因为如果每次使用不同的行属性添加多次,则同一变体 ID 可能存在于多行中。但是,保证购物车中每行的密钥都是唯一的。
因此,请求示例如下:
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: {
updates:{
"100000:abcdef":0, // Use the line-item key inside the quotes
"100001:xyzwnm":0
}
},
dataType: 'json',
async:false, // Be warned, async:false has been deprecated in jQuery for a long time and is not recommended for use. It's generally recommended to use callbacks or promises instead
});
创建
updates
数据的一种方法是通过简单的 for 循环。假设您将购物车的当前内容保存到名为 cart
的变量中,可能如下所示:
var updateData = {}
for(var i=0; i < cart.items.length; i++){
var item = cart.items[i];
if( /* Check for item that needs to be removed */){
updateData[item.key] = 0;
}
}
// Now you can make your AJAX call using this updateData object
array.reduce
来做到这一点:
var updateData = cart.items.reduce(function(acc, item){
if( /* Check for item that we want to remove */){
acc[item.key] = 0
}
return acc;
}, {})
// Now make your AJAX call using the updateData that we created
无论哪种方式,我们最终的 AJAX 调用现在看起来都是这样的:
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: {
updates: updateData
},
dataType: 'json',
success: function(cart){ console.log('Hooray!', cart) },
error: function(err){ console.error('Booo!', err) }
});
希望这有帮助!
从购物车中删除单个商品。
function removeItemFromCart (item) {
fetch('/cart/update.js', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
updates: {
[item.key]: 0
}
})
}).then(response => response.json())
.then((newCart) => {
console.log('Updated cart:', newCart)
})
.catch(console.error)
}
我编写了一个函数,根据商品的变体 ID 从购物车中删除商品,无需使用 jQuery。我希望它有帮助。
function removeFromCartByVariantID(variantID) {
// Fetch the current cart data
fetch('/cart.js')
.then(response => response.json())
.then(cartData => {
const lineItem = cartData.items.find(item => item.variant_id === parseInt(variantID));
if (lineItem) {
fetch('/cart/update.js', {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
updates: {
[lineItem.key]: 0
}
}),
})
.then(response => {
if (!response.ok) {
throw new Error('Request failed with status ' + response.status);
}
console.log('Successfully removed item from the cart');
})
.catch(error => {
console.error('Error occurred:', error);
});
} else {
console.log(`Item with variant ID ${variantID} not found in the cart`);
}
})
.catch(error => {
console.error('Error fetching cart:', error);
});
}
用途:
removeFromCartByVariantID(39351307173911)