Remove a specific item when on the home page

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!

Solutions

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

Similar questions

Change cart item price if a specific item is in the cart on Woocommerce
I am using WooCommerce Memberships and want to extend a free membership to first time members if they purchase a specific product. I can get some of this to work separately, but am having trouble getting it to all come together. There is also a sale price on the item they have to purchase, so I am also checking the dates to see if the item is in th...
Add conditionnaly note for specific cart item and order item in woocommerce
I would like to add note for cart, order received, order view, Order pay and email notifications for a specific product. I would like to display note if this specific product has only the '_wapbk_booking_date' specific meta key. If I use this filter "woocommerce_order_item_class" this snippet works but with a display error at the end of the text, I...
How to restrict WooCommerce cart item quantity to one for specific item
In my custom Woocommerce template I want to restrict the quantity of a product to 1. I'm working from Empty cart upon page load allowing add to cart in WooCommerce answer to my previous question. This is what I have: It kind'a works. But I want checkout on the same page, and with this code checkout does not update when product is added to cart.
Remove WooCommerce cart quantity selector from cart page only if cart item has specific meta data
I have found this answer which can remove the cart quantity selector as needed, however I only need to do this if a cart item has specific meta data. As I need to check for cart item meta rather than product meta, I can't use answers like this one. How can I update the below code to only apply if the item has the cart meta data 'Ticketnumber'? I th...
In Woocommerce -Redirect to shop page after removing last item from the cart page when item is removed through ajax
How to redirect page to shop page when last item removed from cart in woocommerce using ajax? I am tried below code:
Allow users to edit home page from WordPress (home.php problems)
I am wondering how do you create a home page with a custom design like banners different layout from other pages while allowing users to edit it. I used to use home.php problem is user cannot edit it ... unless they know PHP. An option will be to create shortcodes for each section of the home page eg. [banner], [services] or even an empty tag [late...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.