I am trying to hook in on the Add to cart redirect, changing the URL for specific product-ID As a starting point, I found the following code (Source: https://jeroensormani.com/redirect-users-after-add-to-cart/)
function my_custom_add_to_cart_redirect( $url ) {
if ( ! isset( $_REQUEST['add-to-cart'] ) || ! is_numeric( $_REQUEST['add-to-cart'] ) ) {
return $url;
}
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_REQUEST['add-to-cart'] ) );
// Only redirect the product IDs in the array to the checkout
if ( in_array( $product_id, array( 1, 16, 24) ) ) {
$url = "https://mywebsite.com/";
}
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
Which seems approved by others. For me, it seems to ignore the redirect infusion when a condition is applied to it. It works when the condition is left out:
function my_custom_add_to_cart_redirect( $url ) {
$url = "https://mywebsite.com/";
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );
My conclusion/guess is, that there is something in the condition or the filter, that is outdated, but I cannot figure out what's wrong. I have researching other simuler code, based on the woocommerce_add_to_cart_redirect hook, and tried countless snippets, and combinations of functions, but I cannot get any of them working, whenever a condition is applied to it.
[Edit] Now that I think about it, It could very well be a query problem. But not too sure how to go about that.
Any help will be greatly appreciated.