skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    Fatal error: Uncaught ArgumentCountError: Too few arguments to function pza_custom_add_to_cart_redirect(), 1 passed in /home/DOMAIN/public_html/wp-includes/class-wp-hook.php on line 287 and exactly 3 expected in /home/DOMAIN/public_html/wp-content/themes/generatepress_child/functions.php: 149 Stack trace: #0 /home/DOMAIN/public_html/wp-includes/class-wp-hook.php(287): pza_custom_add_to_cart_redirect('https://DOMAIN...') #1 /home/DOMAIN/public_html/wp-includes/plugin.php(212): WP_Hook->apply_filters('https://DOMAIN...', Array) #2 /home/DOMAIN/public_html/wp-content/plugins/uni-woo-custom-product-options-premium/includes/class-uni-cpo-frontend-scripts.php(959): apply_filters('woocommerce_add...', 'https://DOMAIN...') #3 /home/DOMAIN/public_html/wp-includes/class-wp-hook.php(287): Uni_Cpo_Frontend_Scripts::load_scripts('') #4 /home/DOMAIN/public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #5 /home/DOMAIN/public_ in /home/DOMAIN/public_html/wp-content/themes/generatepress_child/functions.php on line 149

    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.


  2. 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:

    • Ajax add to cart,
    • and Normal add to cart.

    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:

    'cart_url' => apply_filters( 'woocommerce_add_to_cart_redirect', wc_get_cart_url(), null ),
    

    where the 2nd argument is set to null.


    For Normal Add to cart the hook is defined in here like:

    $url = apply_filters( 'woocommerce_add_to_cart_redirect', $url, $adding_to_cart );
    

    where $adding_to_cart (2nd argument) is the product object defined in here like:

    $adding_to_cart = wc_get_product( $product_id );
    

    Here is a correct example of working code for Normal Add to cart handling specific product Ids custom redirection:

    add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect', 10, 2 );
    function custom_add_to_cart_redirect( $redirect_url, $product ) {
        $product_ids = array( 37, 22, 53, 40 ); // Here set your product Ids in the array
    
        // Only redirect the product IDs in the array to the checkout
        if ( is_object($product) && in_array( $product->get_id(), $product_ids ) ) {
            $redirect_url = wc_get_checkout_url();
        }
    
        return $redirect_url;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works for normal add to cart.

    To handle Ajax add to cart redirection to a custom Url for specific products, Some jQuery code will be required…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search