【问题标题】:WP_Query and WooCommerce orders with pending status issue具有待处理状态问题的 WP_Query 和 WooCommerce 订单
【发布时间】:2017-06-16 13:37:15
【问题描述】:

我无法获取状态为 wc-pending / Pending Payment 的订单对象。它只是返回所有订单对象:

$my_course_query = new WP_Query( array(
    'post_type'     => 'shop_order',
    'post_status'   => 'wc-pending',
    'posts_per_page'      => -1
) );

【问题讨论】:

    标签: php wordpress woocommerce foreach orders


    【解决方案1】:

    您的代码在前端工作正常,我已经对其进行了测试,它只输出带有 **pending 状态的订单。由于您的问题不详细,所以我无法判断您的问题是什么。

    我在WordPress WP_Query reference 上发现了这个可能有用的注释:
    注意:Ticket #18408 对于在后台查询帖子,请考虑使用 get_posts(),因为 wp_reset_postdata() 可能无法按预期运行。

    一般来说,我不使用 WP_Query() 来处理客户订单,但 wc_get_orders() (或 get_posts() 也是)方式:

    $customer_orders = wc_get_orders( array(
        'limit'    => -1,
        'status'   => 'pending'
    ) );
    
    // Iterating through each Order with pending status
    foreach ( $customer_orders as $order ) {
    
        // Going through each current customer order items
        foreach($order->get_items() as $item_id => $item_values){
            $product_id = $item_values['product_id']; // product ID
    
            // Order Item meta data
            $item_meta_data = wc_get_order_item_meta( $item_id );
    
            // Some output
            echo '<p>Line total for '.wc_get_order_item_meta( $item_id, '_line_total', true ).'</p><br>';
        }
    }
    

    这也适用于获取订单对象。

    相关文档:wc_get_orders and WC_Order_Query

    【讨论】:

    • 嗨,知道如何在使用 wc_get_orders 时避免获取 WC_Order_Refund 对象吗?实际上我依赖于循环内的测试(例如:$order-&gt;get_type() !=='shop_order_refund')但我想知道是否有更好的方法在查询中使用一些 magic
    • @TemaniAfif 添加参数type 喜欢'type' =&gt; 'shop_order' 以避免退款订单...
    • 好的,谢谢,我不知道我是怎么从文档中漏掉的 -.- 我专注于状态参数。
    【解决方案2】:

    我通过简单地使用自定义查询解决了这个奇怪的问题。

    不知何故添加'post_status' =&gt; 'wc-pending'实际上并没有改变查询,但如果我使用'post_status' =&gt; 'pending',查询就会改变。

    所以我所做的是使用该自定义查询并将pending 修改为wc-pending

    【讨论】:

      【解决方案3】:

      我在调试时确实遇到了同样的问题(返回所有订单)。

      将调试代码包装成一个动作有助于输出预期的数据:

      add_action( 'init', 'debug_init' );
      
      function debug_init() {
      
          $custom_query_args = array(
              "fields" => "ids",
              "post_type" => "shop_order",
              "post_status" => array('wc-processing'),
              "posts_per_page" => "-1",
              "offset" => "0",
              "date_query" => [
                      "before" => "2020-09-10 23:59",
                      "after" => "1970-01-01 00:00",
                      "inclusive" => "1"
              ],
              "order" => "DESC"
          );
      
      
          $debugQuery = new WP_Query( $custom_query_args );
          $order_ids   = $debugQuery->posts;
      
          print_r($order_ids);
      
          die();
      }
      

      【讨论】:

        猜你喜欢
        • 2018-02-09
        • 2023-03-27
        • 2013-12-23
        • 2016-08-04
        • 2017-12-25
        • 2022-10-05
        • 1970-01-01
        • 2019-05-06
        • 1970-01-01
        相关资源
        最近更新 更多