Menu Close

Remove Product From Cart Programmatically in WooCommerce

Codeamend

Let’s see  how to remove Product From Cart Programmatically from WooCommerce cart items.

This one is useful to remove disabled or inactive products from customer woocommerce cart.

Refer the below snippet…

add_action( 'template_redirect', 'codeamend_remove_cart_product_programmatically' );
 
function codeamend_remove_cart_product_programmatically() {
   if ( is_admin() ) return;
   $product_id = 300;
   $product_cart_id = WC()->cart->generate_cart_id( $product_id );
   $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
   if ( $cart_item_key ) WC()->cart->remove_cart_item( $cart_item_key );
} 
Posted in WooCommerce

You can also read...