简体   繁体   English

WooCommerce - 将商品添加到购物车时刷新商品数量

[英]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.感谢 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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