2017-04-25 120 views
1

在woocommerce电子邮件中添加额外内容时,我尝试获取订单ID,是否有人知道这有什么问题......? 由于如何在Woocomerce中获取订单ID电子邮件

add_action('woocommerce_email_after_order_table', 'add_content', 20); 

function add_content() { 
global $woocommerce; 
echo $order_id 
echo $order->id; 
} 
+0

你绝对不需要'全局变量$ woocommerce;'作为公认的答案...这是过时的,没有必要... – LoicTheAztec

回答

1

你可以传递参数$order并获得$order->get_id();

是这样的:

function add_content($order) { 
echo $order->get_id(); 
} 
+1

我一定是太慢了。 :)但是你不需要全局声明'$ woocommerce',并且如果你需要woocommerce实例,你应该使用'WC()'来代替。 – helgatheviking

1

查找范围的source$order是传递给woocommerce_email_after_order_table钩第一可变。

add_action('woocommerce_email_after_order_table', 'so_43612005_add_content', 20, 4); 

function so_43612005_add_content($order, $sent_to_admin, $plain_text, $email) { 
    // WooCommerce 3.0 
    if(method_exists($order, 'get_id')) { 
     $order_id = $order->get_id(); 
    // WooCommerce 2.6.x 
    } else { 
     $order_id = $order->id; 
    } 
    echo $order_id; 
} 
+1

'method_exists()'好主意,你可以向前和向后兼容的方式。 – helgatheviking

0
add_action('woocommerce_email_after_order_table', 'wdm_add_shipping_method_to_order_email', 10, 2); 
function wdm_add_shipping_method_to_order_email($order, $is_admin_email) { 
    echo '<p><h4>Order Id:</h4> ' . $order->get_order_number() . '</p>'; 
} 
相关问题