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.
2
Answers
Ok, been trying a lot to figure this error out. After adding the working snippet suggested by LoicTheAztec, I get this error, when loading the "Cart" page, regardless of it being empty or not.
It seems to be a conflict with the UNI-CPO Plugin. When disabling the plugin, the issue is gone. I am having a hard time figurering out, if it is the fault of the snippet or the plugin, and how to get around it, if at all possible.
First There are 2 available arguments for
woocommerce_add_to_cart_redirect
filter hook. So yes the code you found is a bit outdated.For everyone, this hook works if you have enabled "Redirect to the cart page after successful addition" option on WooCommerce Settings > Products (tab).
Now, there are 2 kinds of add to cart in WooCommerce:
The hook
woocommerce_add_to_cart_redirect
only works for Normal add to cart to target specific products, as you cant handle the product ID with Ajax add to cart for the redirection (see below).For Ajax add to cart the hook is defined in here like:
where the 2nd argument is set to
null
.For Normal Add to cart the hook is defined in here like:
where
$adding_to_cart
(2nd argument) is the product object defined in here like:Here is a correct example of working code for Normal Add to cart handling specific product Ids custom redirection:
Code goes in functions.php file of the active child theme (or active theme). Tested and works for normal add to cart.