skip to Main Content

I am looking out to redirect user to specific pages which will vary from product to product. So, once user completes payment for Product A, he will be redirected to Link A , which is an external link.
If he buys Product B, he will be redirected to Product B.

If not, At least I want to display dynamic URL on checkout page based on the product, once user completes payment.

Any input for this functionality ?

I tried the Affiliate link/Virtual products in woo commerce but its a different thing ..

3

Answers


  1. Your algoritm:

    1. Get a ID product from your order.
    2. Get permalink find product.
    3. Used wp_redirect(permalink);
    Login or Signup to reply.
  2. The following will redirect to specifics Url based on order item (product Id) once customer complete payment and order has ‘processing’ or ‘completed’ status:

    add_action( 'template_redirect', 'thankyou_custom_redirection', 10, 1 );
    function thankyou_custom_redirection( $order_id ){
        global $wp;
    
        $order_id = get_query_var('order-received');
    
        if( is_wc_endpoint_url('order-received') && ! empty($order_id) && $order_id > 0 ){
    
            // Get the WC_Order Object instance
            $order = wc_get_order( absint($order_id) );
            
            // Allowed order statuses
            $order_statuses = array('processing', 'completed');
            
            if ( is_a($order, 'WC_Order') && in_array($order->get_status(), $order_statuses) ) {
                $settings = product_ids_for_links_settings(); // Load settings
    
                // Loop through order items
                foreach( $order->get_items() as $item ) {
                    $product    = $item->get_product();
                    $product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
                    print_pr($product_id);
    
                    if( in_array( $product_id, array_keys($settings) ) ){
                        $link_redirect = esc_url($settings[$product_id]);
                        break;
                    }
                }
            }
            
            if( isset($link_redirect) && ! empty($link_redirect) ) {
                // (optional) passing variables to URL
                $link_redirect .= '?order_id='.$order_id.'&product_id='.$product_id;
    
                // Redirection
                wp_redirect( $link_redirect );
                exit();
            }
        }
    }
    

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

    Related: Redirect automatically from Woocommerce thankyou to an external link passing variables

    Login or Signup to reply.
  3. I created a simple plugin using the code below to accomplish something similar…

    /*
    Plugin Name: Woocommerce Custom Thank You page per Product
    Description: This plugin will allow you to set up a custom thank you page per product for woocommerce.
    Author: Gabriel Collignon
    */

    add_action( 'woocommerce_thankyou', 'redirect_product_based', 1 ); 
    function redirect_product_based ( $order_id ){
                $order = wc_get_order( $order_id );
            
                foreach( $order->get_items() as $item ) {
                    // Add whatever product id you want below here
                    if ( $item['product_id'] == 181 ) {
                        // URL 
                         wp_redirect( 'https://www....' );
                    } else {
                        // The other URL
                        wp_redirect( 'https://www....' );
                    }
                }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search