I am no expert in Wp and WC hook. I am trying to create a landing page but the rule is when you are on the home page the specific item on the cart will be removed if it exists on the cart
function remove_cbn_on_cart_when_visiting_the_home_page() {
if ( is_page('54542') ) {
$product_id = 54542;
$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 );
}
}
}
add_action( 'template_redirect', 'remove_cbn_on_cart_when_visiting_the_home_page' );
Thank you in advance!
Without getting too far into the logic of the rest of your code, are you SURE the product id is 54542?
It seems that you're expecting both the homepage page ID AND the product ID to be 54542, which probably isn't the case. I'd double check the ID of your homepage and try using that instead. You'll also want to remove the single quotes.
Even better, you could just use if( is_front_page() ){
and not worry about a page ID at all