I'm trying to remove a specific product if it's in the cart when a customer lands on a specific page.
The Page ID is 8688 and the Product ID is 8691 (this product is a variable product, so I want to be sure regardless of the variation in the cart, the entire product is removed if it's in the cart).
This is what I came up with so far:
add_action( 'template_redirect', 'remove_product_from_cart' );
function remove_product_from_cart() {
if( WC()->cart->is_empty() ) return;
if( ! is_page( 8688 ) ) return;
if ( is_admin() ) return;
$product_id = 8691;
$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 );
}
But it doesn't really works and but I feel totally lost. Appreciate any and all assistance.