【问题标题】:List all completed WooCommerce orders in the front end在前端列出所有已完成的 WooCommerce 订单
【发布时间】:2020-10-04 16:23:38
【问题描述】:

所以我也是在 Wordpress 和 woocommerce 中编码的新手!

我想在 Wordpress 网站的前端列出所有已完成的订单、订单号和订购的商品。

我创建了一个模板文件,我可以让它运行基本的 PHP,例如 echo 和 foreach,但我不知道如何列出已完成的订单!

但是,下面的代码不返回任何订单。

我发现注释掉的 SQL 语句更符合我的要求,即返回文本元字段中日期为 x 的所有订单。但我不知道如何将它包含在模板文件中!

<?php
/**
* Template Name: PPR List
*/
wp_head();
 
 Echo "testing";

//"SELECT post_id FROM wp_postmeta WHERE meta_key = 'arrival_date' AND meta_value <= $today"
$all_orders = get_posts( array(
    'numberposts' => $order_count,
    'post_type'   => wc_get_order_types( 'complete' ),
    'post_status' => array_keys( wc_get_order_statuses() )
) );

echo '<pre>'; print_r($array); echo '</pre>';

wp_footer();

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    使用WC_Order_Query是一种更轻更简单的方式

    wc_get_ordersWC_Order_Query 提供了一种检索订单的标准方法,该方法可以安全使用,并且不会因未来 WooCommerce 版本中的数据库更改而中断。

    来源:wc_get_orders and WC_Order_Query


    所以你得到

    $args = array(
        'status'       => 'completed', // Accepts a string: one of 'pending', 'processing', 'on-hold', 'completed', 'refunded, 'failed', 'cancelled', or a custom order status.
        'meta_key'     => 'arrival_date', // Postmeta key field
        'meta_value'   => $today, // Postmeta value field
        'meta_compare' => '<=', // Possible values are ‘=’, ‘!=’, ‘>’, ‘>=’, ‘<‘, ‘<=’, ‘LIKE’, ‘NOT LIKE’, ‘IN’, ‘NOT IN’, ‘BETWEEN’, ‘NOT BETWEEN’, ‘EXISTS’ (only in WP >= 3.5), and ‘NOT EXISTS’ (also only in WP >= 3.5). Values ‘REGEXP’, ‘NOT REGEXP’ and ‘RLIKE’ were added in WordPress 3.7. Default value is ‘=’.
    );
    
    $orders = wc_get_orders( $args );
    
    foreach ( $orders as $order ) {
        $order_id = $order->get_id();
        echo '<p>' . $order_id . '</p>';
    }
    

    可能会派上用场:How to get WooCommerce order details

    【讨论】:

    • 非常感谢,这对我来说真的很有意义,不像我见过的其他例子。
    猜你喜欢
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2019-03-03
    • 2017-05-14
    相关资源
    最近更新 更多