2017-10-04 144 views
1

我在woocomerce 3+上创建了html电子邮件模板,但无法通过订单ID获取订单项。我尝试过,但没有为我工作。获取订单项woocommerce订单

<?php 
     $order = wc_get_order($order_id); 
     $order_items = $order->get_items(); 
      foreach ($order_items as $item_id => $item_data) { 
      $product_name = $item_data['name']; 
      $item_quantity = $order->wc_get_order_item_meta($item_id, '_qty', true); 
      $item_total = $order->wc_get_order_item_meta($item_id, '_line_total', true); 

      echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. $item_total; 
     } 
?> 

任何帮助非常感谢。

谢谢。

+0

尝试最重要WooCommerce电子邮件模板的'$ order'对象存在,**而不是订单ID **,所以你应该尝试更换'$为了= wc_get_order ($ order_id);''通过'global $ order;'...它应该起作用。现在你的问题并没有详细描述你已经完成了什么以及在哪里...... – LoicTheAztec

回答

0

与下面的代码

add_action('init','orderLineItem'); 
function orderLineItem() 
{ 
    $orderId = 523; //put your dynamic order id or static id 
    $order = wc_get_order($orderId); 
    foreach ($order->get_items() as $item_key => $item_values) { 
     $item_data = $item_values->get_data(); 
     echo $product_name = $item_data['name']; 
     echo $quantity = $item_data['quantity']; 
     echo $line_total = $item_data['total']; 
    } 
}