Looking to remove specific WooCommerce products from a cart? All you need is to get the cart’s item key and remove accordingly.
1 answers
1
Remove cart items conditionally
In order to remove a specific item from your WooCommerce cart, you must loop through the cart, get the item key and wrap the remove_cart_item() function with a product ID dependant if statement, as per the below example.
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( $cart_item['product_id'] == $your_product_id_here ) {
WC()->cart->remove_cart_item( $cart_item_key );
}
}
Note, you can replace WC() with $woocommerce. However, be sure to use global $woocommerce at the top of your function when using this.
— Support WizardAnswer 1