【问题标题】:Get processing status orders count in WooCommerce?在 WooCommerce 中获取处理状态订单计数?
【发布时间】:2018-06-04 12:57:19
【问题描述】:

我想获取 WooCommerce 中的处理订单数。我在 Code Snippet 插件中使用了以下代码,但这是有效的。

if( !function_exists( 'wc_processing_order_count' ) ) { 
    require_once '../plugins/woocommerce/includes/wc-order-functions.php'; 
}


// NOTICE! Understand what this does before running. 
$result = wc_processing_order_count(); 

它什么也没返回。

【问题讨论】:

    标签: php sql wordpress woocommerce orders


    【解决方案1】:

    以上两个答案对于这么简单的事情来说都太复杂了。

    最简单的方法是这样:)

    $processing_orders_count = count(wc_get_orders( array(
        'status' => 'processing',
        'return' => 'ids',
        'limit' => -1,
    )));
    

    【讨论】:

    • 我认为这是一个好方法。还有一个额外的)。
    【解决方案2】:

    此自定义函数使用非常简单的 SQL 查询来获取特定状态的订单数:

    function get_orders_count_from_status( $status ){
        global $wpdb;
    
        // We add 'wc-' prefix when is missing from order staus
        $status = 'wc-' . str_replace('wc-', '', $status);
    
        return $wpdb->get_var("
            SELECT count(ID)  FROM {$wpdb->prefix}posts WHERE post_status LIKE '$status' AND `post_type` LIKE 'shop_order'
        ");
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


    使用示例“处理”订单计数:

    // Display "processing" orders count
    echo get_orders_count_from_status( "processing" );
    

    【讨论】:

      【解决方案3】:

      这个方法可以帮到你。订单存储为 post_type shop_order。因此,通过创建查询以获取所有 shop_order 类型的帖子并通过传递参数以获取所有处理订单,您将能够获取这些订单

          $args = array(
              'post_type'         => 'shop_order',
              'post_status'       => 'publish',
              'posts_per_page' => -1,
              'tax_query' => array(
                       array(
                           'taxonomy' => 'shop_order_status',
                           'field' => 'slug',
                           'terms' => array('processing')
                       )
               )
          );
      
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ){ 
              $loop->the_post();
              $order_id = $loop->post->ID;
              $order = new WC_Order($order_id);
         }
      

      【讨论】:

        【解决方案4】:

        看看:https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query 在类似的情况下,该参考确实帮助了我。 与其他答案中提到的使用硬编码查询或 WP_Query 相比,使用 WC 函数可确保您的代码更具前瞻性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-04
          • 2013-12-23
          • 2018-02-09
          • 2017-05-24
          • 1970-01-01
          • 2018-05-26
          • 1970-01-01
          • 2021-09-04
          相关资源
          最近更新 更多