我正在商店页面上寻找我的 woocommerce 商店的代码片段,当数量更改时,无需单击“添加到购物车”即可将产品添加到购物车。
示例: 客户在商店页面(产品存档)中想要 5 杯,默认情况下数量字段应为 0。然后客户在数量字段中输入 5,而不单击“添加到购物车”,购物车中应自动有 5 杯.
当顾客改变主意并想要 3 杯时,他可以返回,在数量字段中输入 3 而不是 5,而无需单击“添加到购物车”,现在他的购物车中应该有 3 杯
基本上将数量字段与购物车同步。
我尝试了以下代码:否定响应
jQuery(function($){
// Listen for changes in the quantity input field
$('body').on('change', '.quantity input', function(){
var product_id = $(this).closest('li.product').data('product_id');
var quantity = $(this).val();
$.ajax({
type: 'POST',
url: wc_add_to_cart_params.ajax_url,
data: {
action: 'woocommerce_add_to_cart',
product_id: product_id,
quantity: quantity,
},
success: function(response) {
if (response.error && response.product_url) {
window.location = response.product_url;
return;
} else {
$(document.body).trigger('added_to_cart');
}
},
});
});
});