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
  • 229,944
  • 23
  • 356
  • 399
brandonthebuck
  • 65
  • 1
  • 1
  • 4
  • 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

9

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
  • 229,944
  • 23
  • 356
  • 399