2

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');
LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
  • Write more details what are you looking for? – Gufran Hasan Apr 02 '18 at 04:37
  • First of all in a shortcode you do not `echo` but `return`. Where is this shortcode going to be used? In the checkout page? To show other orders? – Omar Tanti Apr 02 '18 at 04:40
  • That is simply NOT possible as **Order ID doesn't exist in checkout**, It's generated only after customer has "placed order"… – LoicTheAztec Apr 02 '18 at 09:50
  • @OmarTanti I want to have it in the order-received page, so the transaction has been approved, and the Order ID has been generated. But I want to dynamically name files on S3 with the same Order ID to keep files consistent, and shortcodes can function within URL parameters. – brandonthebuck Apr 02 '18 at 16:43
  • @LoicTheAztec This is in the order-received, so the transaction has been approved. – brandonthebuck Apr 02 '18 at 16:59
  • @brandonthebuck That was not clear, so I have updated your question and answered… – LoicTheAztec Apr 02 '18 at 17:57

1 Answers1

7

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.

LoicTheAztec
  • 207,510
  • 22
  • 296
  • 340
  • Thanks! Can this be modified to display the order_key? – brandonthebuck Apr 02 '18 at 20:35
  • @brandonthebuck Added an additional code for that after… – LoicTheAztec Apr 02 '18 at 20:42
  • Brilliant! Great!! I searched for `$wp->query_vars['order-received']` for ages!! @LoicTheAztec, where did you get this? in any case - thank you so muuch!! – Able Oct 11 '18 at 22:38
  • would $wp->query_vars['order-received'] always be order-received, if you changed the endpoint name would this be the customised end point name instead? – bigdaveygeorge Nov 15 '20 at 11:45
  • Does this work even if the slug was translated using WPML for instance? – alev Apr 03 '21 at 15:34
  • @alev Meta key should not be translated!… instead use `__()` function when saving the value. Then you will be able to translate the value as a translatable string. – LoicTheAztec Apr 03 '21 at 18:49