2014-06-23 86 views
0

我的WooCommerce定制中有一个反复出现的问题:如何获取函数内的当前订单ID。将WooCommerce订单ID传递到钩子

一些钩子可以通过$ order_id传递它,但是在特定订单的用户帐户页面的上下文中,例如下面的代码不起作用。

问题是$order_id是空的,当然。但是,如何将订单的ID传递给此函数?

add_action('woocommerce_order_items_table', 'xcsn_woocommerce_order_items_table'); 
function xcsn_woocommerce_order_items_table ($order_id) { 

    $order = new WC_Order($order_id); 

    echo 'The order ID is: ' . $order->get_order_number(); 

    // or 

    echo '<br>The order ID is: ' . $order->id; 

} 

我尝试这样做,但它返回用户的ID号,不是顺序:

global $woocommerce; 
$order = new WC_Order($post->ID); 
echo 'The Order ID: ' . $order->get_order_number(); 

有什么建议?

回答

0

这解决了上面的具体问题(虽然不是一般的问题):

add_action('woocommerce_order_items_table', 'xcsn_woocommerce_order_items_table'); 
function xcsn_woocommerce_order_items_table ($order) { 

    echo $order->id; //This is the order id, even in the user Accounts>Order page 

} 
0
add_action('woocommerce_order_items_table', 'xcsn_woocommerce_order_items_table',10,2); 
function xcsn_woocommerce_order_items_table ($order_id) { 
    $order = new WC_Order($order_id); 
    echo 'The order ID is: ' . $order->get_order_number(); 
    // or 
    echo '<br> 
    The order ID is: ' . $order_id; 

} 

检查这一点,你需要添加10,2上的动作,它返回后的订单ID成功提交订单,没有这个功能将提交订单之前运行,所以订单编号可能0null

+0

它不会返回任何东西。 – huykon225

相关问题