Get the Order ID in Woocommerce order received page as shortcode

℡╲_俬逩灬. 提交于 2021-02-07 10:24:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!