I guess you should use your function on the hook woocommerce_add_to_cart
instead of template_redirect
Your function works fine for me. Try an alternative methow which also works:
add_action('template_redirect', function () {
$product_id = 29690;
foreach( WC()->cart->get_cart() as $key=>$item){
if($item['product_id']==$product_id){
WC()->cart->remove_cart_item( $key );
}
}
// $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 );
});
double check your $product_id and possibly var_dump something to refine debugging
Related Posts:
- Adding an action within a function that is being called by add_filter
- Remove action on product archive page [closed]
- How to remove_action inside class [duplicate]
- Is there a Woocommerce hook that fires when applying a coupon but before checking if it’s valid?
- What is the action hook for an order that fails on frontend checkout in WooCommerce?
- Remove action added through function
- Which hook should I use to capture $_POST(‘password’) via profile update and password reset
- How can I add an additional action button into the woocommerce admin order page?
- Edit WooCommerce product content based on category
- How to list all WC products by their attributes values?
- WooCommerce Hook: Content get’s displayed twice and not AFTER the order total
- woocommerce_new_product action doesn’t fire
- password_reset doesnt work
- Woocommerce – Adding row to cart table
- Functions.php change for Woocommerce not working
- Display custom product option in admin order details
- Assign user role by text field in WordPress (Woocommerce) [closed]
- Combine one action and one filter
- How can I send information when a order is completed to my own POS?
- Parameters in woocommerce_before_add_to_cart_button hook
- How to change lost password email text using custom plugin wordpress?
- Overriding Plugin function in your child theme
- How can I locate where the actions are defined? [duplicate]
- add_action don’t updates theme layout when using values from the WordPress customizer
- Woocommerce add a text field if specific option is selected
- add_filter not works in ajax
- How Do I Access Parameters Emitted by an Action?
- Reposition variation stock message
- How can I define a custom template for woocommerce [products] shortcode? [closed]
- Where is the “default attribute” values located in the phpMyAdmin in Woocommerce?
- Display Categories Assigned to a WooCommerce Product
- Get current WooCommerce endpoint not working
- Changing Woocommerce price display [closed]
- Which WooCommerce hook do I need to use to place the coupon field after the checkout sidebar
- WooCommerce Cart – Group Products By Category
- WooCommerce – update order item price and recalculate totals
- How to get woocomerce attributes thumbnail in PHP? [closed]
- Programmatically added variations not showing until clicking Update button
- Editing Header Titles of each details in woocommerce Order Email [closed]
- How to automatically add and show all the product attributes when creating a new product
- Import products to WooCommerce by CSV page won’t open [closed]
- How can i change the design of product sizes [closed]
- Following migration from DEV to LIVE, woocommerce can’t checkout
- How to handle Woocommerce API credentials
- Woocommerce Shipping label just hangs
- adding a script with type=”module” to woocommerce admin area
- user password reset e-mail
- Extending the woocommerce product export functionality
- Woo Related Products How to override the plugin script?
- Woocommerce how to alter shipping tax before checkout
- Line break on Product details [closed]
- WooCommerce add class name in the list [closed]
- How to display Woocommerce variations prices and descriptions
- Adding Transaction-Specific Conversion Tracking from Adwords into WooCommerce
- New field on checkout is shown but it’s not saved on the order details [closed]
- How to rewrite product permalinks in Woocommerce to use category slugs
- Woocommerce altered my homepage, and I can’t figure out how to revert it [closed]
- Woocommerce – Problem sorting and filtering products with different tax rates by price [closed]
- Why does my loop breaks the css below?
- How to add order status class to the body tag?
- Absolute search against wordpress api
- Is it possible to remove editor from a custom product type?
- Show password while you are typing it on the “my account” login page
- /page/ is not working with a custom permalink structure (WooCommerce)
- WooCommerce Bookings Plugin Not Saving Order ID
- Woocommerce Register Error
- Where is this text in a wordpress email coming from? Want to change/remove
- Add starts rating in woocommerce product comment from backend
- Need to check if user has completed a woo commerce payment
- Woocommerce show field in product if product-tag is “VET” else hide
- How to make a “subtitle” for each Woocommerce product
- Create Product with Woocommerce API, but Variations are not linked
- How to reduce checkout loading time of `?wc-ajax=checkout`
- unable to customize query to get filtered products by meta_key
- Woocommerce + Google Analytics (Conversion Rate Not Working)
- send order detail to swiftpos
- Update user meta when customer update their first name
- Woocommerce API security concerns
- How to add a wrapper element on image for products page
- Dynamic subdomains for woocommerce affiliate sales but NO multisite. Is that possible?
- Woocommerce posts and products links works only once and then get 404 error
- Limit image resolution on upload file for specific field
- Woocommerce pagination problem- page/2 = 404 solution
- Woocommerce , how to set billing_address via JS?
- Using admin email in billing address
- Enable Payment gateway if ACF checkbox in user profile is checked
- How do you reactive related products on WooCommerce when it has been disabled?
- Display product variations in shop page
- Shortcodes in billing fields doesn’t work
- Update a user meta key based on WooCommerce product purchase
- How to fix spacing in address tab in my account page
- show multiple product photos on WooCommerce single page
- How to get _POST variable on WooCommerce product page (form on homepage)
- How do I put woocommerce cart page to my checkout page? [closed]
- Search box missing Woocommerce Product Bundle Extension
- DatePicker in Woocommerce (My Account) Registration [closed]
- problems with an object product wooCommerce [closed]
- CSS url rules not relative to css path in account endpoint areas. IE. subscriptions
- Critical error on woocommerce checkout page
- Can’t add variable products since update WooCommerce [closed]