TDV
TDV

Reputation: 23

How to append order number to a URL on the "Thank You"-page in WooCommerce

I'm looking to do something quite simple which I can't get figured out. I want to append (add) the order number to a URL when a customer reaches the Thank You page after paying in WooCommerce.

It needs to look a bit like: https://example.com/schedule?ordernumber=123456

This way I can build a scheduling page that takes the ordernumber and pre-fills it into the form where a customer would plan a meeting for the product without them having to remember the order number.

I tried simple things as echo 'Schedule your appointment: <a href="https://example.com/schedule?ordernumer=' . $order->get_order_number(); . '" /a>'

But that did not work out. I've been trying for a while now, so I hope someone can help me out,

Thanks a lot and stay safe!

Upvotes: 2

Views: 690

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You can simply use the following that will add a linked button on "Order received" Thankyou page. The button link will be like www.example.com/schedule?ordernumber=123456.

The code:

add_action( 'woocommerce_thankyou', 'custom_thankyou_linked_button', 5 );
function custom_thankyou_linked_button( $order_id ){
    $order = wc_get_order( $order_id );

    printf( '<p><a href="%s" class="button">%s</a></p>',
        home_url('/schedule?ordernumer=') . $order->get_order_number(),
        __("Schedule your appointment", "woocommerce")
    );
}

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

Note: To make it appear aftter order details change the hook priority from 5 to 20.

enter image description here

Upvotes: 1

Related Questions