WP SITES

3084 Coded Tutorials & 287 Plugins

Redirect After Checkout Based on Specific Product ID

This code will redirect to a custom URL after checkout based on the array of product ids in the code.

add_action('woocommerce_thankyou', 'redirect_to_url_based_on_product_id');
function redirect_to_url_based_on_product_id( $order_id ) {

    if ( ! $order_id ) 
    
    return;

    $order = wc_get_order($order_id);
    
    $survey_product_ids = [387]; 
    
    $survey_url = 'https://example.com'; 

    foreach ($order->get_items() as $item) {
    
        if (in_array($item->get_product_id(), $survey_product_ids)) {
        
            wp_redirect($survey_url);
            
            exit;
        }
    }
}

Add or change 1 or more product ids in this line :

$survey_product_ids = [387, 888, 999]; 

Leave a Reply

New Plugins