WooCommerce在我的帐户中显示订单产品名称

时间:2014-08-22 04:22:42

标签: php wordpress woocommerce

我正在使用WooCommerce 2.0,我想在myaccount.php页面中检索并显示订购的产品名称以及订单号。

因此,如果这是myaccount.php中的默认显示:

ORDER   DATE    STATUS  TOTAL   ACTIONS LICENSING
#521    August 19, 2014 Completed   $99.99 for 1 item   VIEW

我想将其更改为:

ORDER               DATE    STATUS  TOTAL   ACTIONS LICENSING
#521-ProductName    August 19, 2014 Completed   $99.99 for 1 item   VIEW

有人可以提供有关如何检索订单产品名称的任何建议吗?我对如何做到这一点很困惑。

谢谢!

2 个答案:

答案 0 :(得分:4)

您可以在my-orders.php

中进行一些更改后执行此操作

你需要my-orders.php。将副本放在主题文件夹中以使其更新校样。

//Add the following code  in the customer_order loop

foreach($order->get_items() as $item) {
    $product_name = $item['name'];

}

<?php echo $product_name;?> //echo product name

答案 1 :(得分:0)

$args = array(
  'post_type' => 'shop_order',
  'post_status' => 'publish',
  'meta_key' => '_customer_user',
  'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);

$customer_orders = $my_query->posts;

foreach ($customer_orders as $customer_order) {
 $order = new WC_Order();

 $order->populate($customer_order);
 $orderdata = (array) $order;

 // $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}

为了获得订单的更多细节,您可以使用以下代码。

假设$ post-&gt; ID是您要显示包含订单的产品的ID,这就是您所需要的:

$products = array();
foreach (get_posts('post_type=shop_order&numberposts=-1&post_status=publish') as $order) {
    $order = new WC_Order($order->ID);
    foreach($order->get_items('line_item') as $item) {
        $product_id = (!empty($item['variation_id'])) ? $item['variation_id'] : $item['product_id'];
        $products[] = $product_id;
    }
    if (in_array($post->ID,$products)) {
        echo 'Status: '.$order->order_status;                         
        echo '<br>Date  : '.$order->order_date;                           
        echo '<br>Email  : '.$order->billing_email; 
    }   
}