Woocommerce custom redirection based on purchased product after payment

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


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....' );
                }
            }
}