2015-12-08 76 views
0

當用戶選擇數量並點擊添加到購物車按鈕時,該商品將以正確的數量添加到購物車。但是,如果用戶再次點擊添加到同一個項目但添加了不同數量的項目,則會將其添加到原始數量。WooCommerce - 當商品添加到購物車時刷新商品數量

我想要發生的是要刪除並用新項目數量更新的原始項目數量。

這怎麼可能?

+0

您需要腳本的產品「從購物車中刪除」如果它已經存在,然後「添加到購物車」 –

+1

對不起,我不明白? –

+0

您需要的是一段代碼,以檢查產品是否已經存在於購物車中,如果存在,請從購物車中刪除產品並添加新的信息 –

回答

0

感謝Lucky Chingi的幫助,我設法使它工作。

add_filter('woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before'); 

function woo_custom_add_to_cart_before($cart_item_data) { 
$cart = WC()->instance()->cart; 
$id = $_POST['product_id']; 
$cart_id = $cart->generate_cart_id($id); 
$cart_item_id = $cart->find_product_in_cart($cart_id); 

if($cart_item_id){ 
$cart->set_quantity($cart_item_id,0); 
} 
return true; 
} 
相關問題