Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the Order ID in Woocommerce order received page as shortcode

I want to have WooCommerce Order ID's referenced as a shortcode to be easily be used in the Order received page to generate dynamic links.

function my_order_id( $atts ) {
    echo $order->get_id();
}
add_shortcode( 'my_order_id', 'my_order_id');
like image 478
brandonthebuck Avatar asked Jan 25 '26 10:01

brandonthebuck


1 Answers

Here is the way to get the Order ID in "Order received" (thankyou) page as a shortcode:

function get_order_id_thankyou( $atts ) {
    // Only in thankyou "Order-received" page
    if( ! is_wc_endpoint_url( 'order-received' ) )
        return; // Exit

    global $wp;

    // Get the order ID
    $order_id  = absint( $wp->query_vars['order-received'] );

    if ( empty($order_id) || $order_id == 0 )
        return; // Exit;

    // Testing output (always use return with a shortcode)
    return '<p>Order ID: ' . $order_id . '</p>';
}
add_shortcode( 'my_order_id', 'get_order_id_thankyou');

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


The same for the order key:

function get_order_key_thankyou( $atts ) {
    // Only in thankyou "Order-received" page
    if( ! is_wc_endpoint_url( 'order-received' ) )
        return; // Exit

    global $wp;

    // Get the order ID
    $order_id  = absint( $wp->query_vars['order-received'] );

    if ( empty($order_id) || $order_id == 0 )
        return; // Exit;

    // Testing output (always use return with a shortcode)
    return '<p>Order Key: ' . get_post_meta( $order_id, '_order_key', true ) . '</p>';
}
add_shortcode( 'my_order_key', 'get_order_key_thankyou');

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

like image 56
LoicTheAztec Avatar answered Jan 27 '26 00:01

LoicTheAztec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!