获取 Woocommerce 订单接收页面中的订单 ID 作为简码

Get the Order ID in Woocommerce order received page as shortcode

我想将 WooCommerce 订单 ID 引用为短代码,以便在订单接收页面中轻松使用以生成动态链接。

function my_order_id( $atts ) {
    echo $order->get_id();
}
add_shortcode( 'my_order_id', 'my_order_id');

以下是在 "Order received" (thankyou) 页面中获取订单 ID 作为简码的方法:

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');

代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。


订单键相同:

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');

代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。