Custom Order received return URL query args in Woocommerce

Wehenever I place an order using Woocommerce I get an URL with a few parameters. It looks like this -> /order-received/12240/?key=wc_order_5bf66c9ea4f0a

I would like to add a few of my own to this URL. But i cant find where this URL is generated.

What i tried:

to add add_query_arg( 'foo', 'bar' ) to the thank-you.php file. Didnt work.

Also tried :

add_action('woocommerce_checkout_order_processed','my_function');
function my_function() {
     add_query_arg( 'foo', 'bar' );
}

Answer

You need to use the dedicated woocommerce_get_return_url filter hook. Somme payment gateways can also use woocommerce_get_checkout_order_received_url filter hook.

add_filter( 'woocommerce_get_return_url', 'customize_get_return_url', 10, 2 );
add_filter( 'woocommerce_get_checkout_order_received_url', 'customize_get_return_url', 10, 2 );
function customize_get_return_url( $return_url, $order ){
    $query_args = array(
        'foo' => 'bar',
        'fruit' => 'apple',
    );
    return add_query_arg( $query_args, $return_url );
}

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

You will get: /order-received/12345/?key=wc_order_ab12cd45efg678&foo=bar&fruit=apple

Leave a Reply

Your email address will not be published. Required fields are marked *