简体   繁体   中英

WooCommerce - Refresh item quantity when item is added to cart

When the user selects their quantity and clicks the add to cart button, the item is added to the cart with the correct quantity. However, if the user clicks add to cart again for the same item but with a different quantity, then that will be added to the original quantity.

What I want to happen is the original item quantity to be removed and updated with the new item quantity.

How would this be possible?

Thanks to the help from Lucky Chingi I managed to make it work.

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;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM