获取 Woocommerce 订单接收页面中的订单 ID 作为短代码 [英] Get the Order ID in Woocommerce order received page as shortcode
本文介绍了获取 Woocommerce 订单接收页面中的订单 ID 作为短代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望将 WooCommerce 订单 ID 作为短代码引用,以便在收到的订单页面中轻松使用以生成动态链接.
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');
推荐答案
以下是获取已收到订单"(谢谢)页面中的订单 ID 作为简码的方法:
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');
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes in function.php file of the active child theme (or active theme). Tested and works.
订单键也是如此:
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 文件中.经测试有效.
Code goes in function.php file of the active child theme (or active theme). Tested and works.
这篇关于获取 Woocommerce 订单接收页面中的订单 ID 作为短代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文