问题
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');
回答1:
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.
来源:https://stackoverflow.com/questions/49605343/get-the-order-id-in-woocommerce-order-received-page-as-shortcode