如何在WooCommerce产品页面上显示详细的订单历史记录

时间:2016-09-06 作者:TripsLeft

我有一个捐赠网站,我正在使用woocommerce。“产品”实际上是不同的非营利组织。我试图在每个产品页面上显示详细的捐赠历史。所以基本上显示一个订单列表,它将显示客户的姓名、日期以及价格。我有这个功能,但如果客户向多个非营利组织捐款(在一个订单上购买多个产品),那么它会显示订单的总金额,而不仅仅是与当前产品匹配的行项目。如有任何建议,将不胜感激。以下是我在“content single product.php”页面上使用的代码:

echo \'<h2>Previous Donations for this Nonprofit</h2>\'; 
$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 \'<br>Date: \'.$order->order_date; 
        echo \'<br>Name: \'.$order->billing_first_name . \'&nbsp;\'  .$order->billing_last_name;
        echo \'<br>Email: \'.$order->billing_email; 
        echo \'<br>Order Total: $\'.$order->get_line_total( $item );      
    }   
}
这是$order变量的var\\u转储 Object(WC_Order)#622 (14) { ["order_type"]=> string(6) "simple" ["id"]=> int(246) ["post"]=> object(WP_Post)#660 (24) { ["ID"]=> int(246) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2016-09-02 13:36:24" ["post_date_gmt"]=> string(19) "2016-09-02 17:36:24" ["post_content"]=> string(0) "" ["post_title"]=> string(42) "Order – September 2, 2016 @ 01:36 PM" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(12) "wc-completed" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(19) "order_57c9b89850215" ["post_name"]=> string(25) "order-sep-02-2016-0536-pm" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2016-09-03 12:08:48" ["post_modified_gmt"]=> string(19) "2016-09-03 16:08:48" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(70) "http://sitename.com/?post_type=shop_order&p=246" ["menu_order"]=> int(0) ["post_type"]=> string(10) "shop_order" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "3" ["filter"]=> string(3) "raw" } ["order_date"]=> string(19) "2016-09-02 13:36:24" ["modified_date"]=> string(19) "2016-09-03 12:08:48" ["customer_message"]=> string(0) "" ["customer_note"]=> string(0) "" ["post_status"]=> string(12) "wc-completed" ["prices_include_tax"]=> bool(false) ["tax_display_cart"]=> string(4) "excl" ["display_totals_ex_tax"]=> bool(true) ["display_cart_ex_tax"]=> bool(true) ["formatted_billing_address":protected]=> string(0) "" ["formatted_shipping_address":protected]=> string(0) "" }

1 个回复
最合适的回答,由SO网友:Jeremy Ross 整理而成

问题是您显示的是订单信息,而不是项目信息。您希望根据项目打印内容,如下所示:

echo \'<h2>Previous Donations for this Nonprofit</h2>\';
foreach (get_posts(\'post_type=shop_order&numberposts=-1&post_status=wc-completed\') as $order) {
    $order = new WC_Order($order->ID);

    foreach($order->get_items(\'line_item\') as $item) {
        if ($post->ID == $item[\'product_id\'] || $post->ID == $item[\'variation_id\']) {
            echo \'<br>Date: \'.$order->order_date; 
            echo \'<br>Name: \'.$order->billing_first_name . \'&nbsp;\'  .$order->billing_last_name;
            echo \'<br>Email: \'.$order->billing_email; 
            echo \'<br>Order Total: $\'.$order->get_line_total( $item );   
        }  
    }
}
我不确定您运行的是哪个版本的WooCommerce,但我在尝试查询post\\u状态为publish的帖子时出错。版本2.2+希望您根据其专有的post状态i进行检查。你可以看到上面的反映。

相关推荐