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
- WooCommerce Webhook Action When a New Product Review was Submitted/Created
- What’s the difference between WC() and $woocommerce
- How to get values from woocommerce admin input fields?
- How to show “Previous Category” and “Next Category” in categories archieve template [closed]
- How to update variable product stock status with code
- Display WooCommerce only in stock sizes product attribute on shop page
- Remove “Choose An Option” on Product Variation & Auto Select Instead
- How to stop a Gutenberg Block from firing multiple GET Requests?
- Woocommerce Product Price Inserted programmatically doesn’t get displayed In Single Product Page
- How to Order by random on a SQL query
- Move add to cart in Woocommerce
- Hide “Delete Note” link in Order Notes Panel
- Exclude products with a stock lower than the low stock threshold from the shop loop [closed]
- Remove Order List Row Link in WooCommerce Admin?
- wp_customize – detect post type to show/hide customizer options
- WooCommerce – Display nested list of all subcategories on archive-product.php
- Set woocommerce product price based on dealer location [closed]
- How can I custom order the product display?
- WordPress plugin cloak link [closed]
- Woocommerce REST API – updating an order changes custom tax calculations
- Showing the WordPress standard text area on a Woocommerce Single Product Page?
- WordPress WooCommerce: How to allow only specified customers to pay by check? [closed]
- How to override the title tag for woocommerce endpoints?
- WooCommerce – Global $product is returning value null
- How to re-arrange this hooked content?
- Pre filter woocommerce products to remove a certain category of products
- WooCommerce Stripe Plugin not showing up in settings [closed]
- Woocommerce – Change the template?
- Wooocommerce disable check_cart_item_stock if cart item has specific meta
- Force meta data on specific product type
- WooCommerce plaintext mail doesn’t display currency symbol
- Woocommerce, finding the ordinal number of a purchase for a certain product
- WooCommerce order refund get qty refunded
- WP/WooCommerce multisite with polylang, product duplication between websites [closed]
- WooCommerce add_to_cart
- WooCommerce Simple Product Options List
- Add custom body tag if product is out of stock
- Woocommerce replace product page image with product tabs [closed]
- How to get all customers cart from woocommerce API?
- WooCommerce login redirect based on cart
- send order detail to swiftpos
- WordPress Woocommerce REST API JSON Error
- data-value for custom woocommerce field showing in code but not visable on page load
- call_user_func_array() expects parameter 1 to be a valid callback, class ‘WC_Rapyd’ does not have a method ‘install’
- How can we find a WooCommerce shop bottleneck? (only when cart is full)
- Conditional for product attribute page
- Setting Page Visibility for WooCommerce Default Page Doesn’t Work
- Deleted products from woocommerce are still in the database
- Remove cart functionality from WooCommerce so it doesn’t remember the products [closed]
- Apply CSS to certain product thumbnails only
- Files names changed (with strange characters) after being uploaded to cpanel
- woocommerce_add_to_cart custom function called several times
- Order Woocommerce Products by On Sale and Alphabetically
- Customer Email Address
- Adding time of order to Admin email for new order
- {status}_{post_type} does not run correctly?
- Uninitialised Parameters using woocommerce_checkout_update_order_meta hook
- Woocommerce customer role doesn’t change if user is already a subscriber [closed]
- Hide add to cart when product variation has no price or is unavailable
- How to get the WooCommercer product variation image
- how add css class to product boxes li, for img, add cart button, decs, price… [closed]
- How can I hide the entire “Shipping Details” block on the admin side of Woocommerce? [closed]
- Decrease stock quantity when a variation is sold using Woocommerce
- Woocommerce – display all sold out products
- Woocommerce how to show most selling products by category
- AffiliateWP not calculating multiple quantities when using affwp_calc_referral_amount filter
- WordPress pointing animated GIF images with wrong file name upon upload (GIFs animation not working)
- Modify existing code to show user nickname in woocoomerce order email instead of user_login
- How to prevent woocommerce product tag field from update?
- WooCommerce custom SVG coloring tool [closed]
- Woocomerce erro shipping [closed]
- How do I prevent double the amount of stock being reduced with this Split Order Woocommerce Code? [closed]