skip to Main Content

I want to redirect to a custom URL after checkout for a single product in Woocommerce, have all other products continue to redirect to the default thank you/order summary page.

I tried using the code snippet in the first answer on this post:

WooCommerce Custom Thankyou redirection based on Product ID

I replaced the product IDs listed as an example in this snippet with the single product ID for the product I want to produce a redirect after checkout for, left the category array blank, and updated the URL. When I tested the checkout redirect for that particular product, but when I tested for other products, they were also redirecting instead of going to the default thank you/order confirmation page.

I know the post is a little old but it says that the code was tested on WooCommerce 3. Is there something I’m missing or should I go another route instead of this snippet?

2

Answers


  1. When you say you left the array blank, you’d be better off removing the array as it’ll return true for every product.

    I.e. has_term() is asking, does this product have a term / (category)? The answer is almost always going to be True in Woocommerce where the products are sat in store categories.

    So, when you see the original code in the answer below, it’s testing against the terms that you set in the $product_categories variable.

    // Iterating through order items and finding targeted products
    foreach( $order->get_items() as $item ){
        if( in_array( $item->get_product_id(), $product_ids ) || has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
            $redirection = true;
            break;
        }
    }
    

    The solution, get rid of the has_term entirely and replace the above section with this:

    // Iterating through order items and finding targeted products
    foreach( $order->get_items() as $item ){
        if( in_array( $item->get_product_id(), $product_ids ) ) {
            $redirection = true;
            break;
        }
    }
    
    Login or Signup to reply.
  2. To enable a redirection after checkout, when only targeted product(s) are exclusively in the order, use the following revisited code instead:

    add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
    function wc_custom_redirect_after_purchase() {
        if ( ! is_wc_endpoint_url( 'order-received' ) ) 
            return;
    
        $targeted_ids = array( 37, 25, 50 ); // <== Targeted product IDs in this array
    
        global $wp;
        
        $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) ); // Order ID
        $order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object
        $targeted_found = $others_found = false; // Initializing
    
        // Iterating through order items and finding targeted products
        foreach( $order->get_items() as $item ){
            if( in_array( $item->get_product_id(), $targeted_ids ) ) {
                $targeted_found = true;
            } else {
                $others_found = true;
            }
        }
    
        // Enable redirection when only targeted product(s) are exclusively in the order
        if( $targeted_found && ! $others_found ){
            wp_redirect( home_url( '/your-page/' ) );
            exit;
        }
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

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