I am using the following function to automatically replace the product in the cart if the user adds another product. Its working perfectly. I am trying to add a redirect to checkout after the product has been replaced. But the redirect does not work as I wish.
function custom_maybe_empty_cart( $valid, $product_id, $quantity ) {
if( ! empty ( WC()->cart->get_cart() ) && $valid ){
WC()->cart->empty_cart();
wc_add_notice( 'Only allowed 1 item in cart.', 'error' );
}
return $valid;
wp_redirect( 'https://ils.semarient.io/commander' );
exit;
}
add_filter( 'woocommerce_add_to_cart_validation', 'custom_maybe_empty_cart', 10, 3 );
I've tried different placements for the following lines :
wp_redirect( 'https://ils.semarient.io/commander' );
exit;
1 ) after the wc_add_notice : nothing happens 2 ) after teturn $valid : nothing happens 3 ) after final brace (before add filter) : endlessly loops to redirect to the page I want and ends in a page crash (too many redirects) > means the redirect is working then ? but isn't at the right place... Code is in the function.php file from child theme
Thanks a lot for any help !