【问题标题】:WooCommerce - Get the number of orders by a given customerWooCommerce - 获取给定客户的订单数量
【发布时间】:2015-08-10 10:28:40
【问题描述】:

我需要找到某个特定客户以前与该商店有过业务往来。

为了实现这一点,我需要找到给定客户的订单数量。

我怎样才能做到这一点?

我尝试了谷歌搜索,但没有找到任何解决方案。

【问题讨论】:

  • 你尝试过什么?你用php吗? (也许你应该用它标记它。)
  • 我查看了他们的 API,但找不到实现目标的方法。是的,WooCommerce 使用 PHP。添加了标签。谢谢

标签: php wordpress woocommerce


【解决方案1】:

只需输入用户 ID,即可获得订单总数:

$numorders = wc_get_customer_order_count( $userid );

为了我自己的目的更进一步,我使用此代码来获取客户未取消订单的数量,因为我不想计算失败的订单尝试次数:

// Get TOTAL number of orders for customer
$numorders = wc_get_customer_order_count( $userid );

// Get CANCELLED orders for customer
$args = array(
    'customer_id' => $userid,
    'post_status' => 'cancelled',
    'post_type' => 'shop_order',
    'return' => 'ids',
);
$numorders_cancelled = 0;
$numorders_cancelled = count( wc_get_orders( $args ) ); // count the array of orders

// NON-CANCELLED equals TOTAL minus CANCELLED
$num_not_cancelled = $numorders - $numorders_cancelled;

【讨论】:

  • 需要添加'limit' => -1
【解决方案2】:

修改@MarkPraschan 代码,这对我很有效,不会发出任何通知,因为我收到了 2 条关于未定义变量 $user_id 的通知,而且我没有通过函数传递代码。使用下面的代码对我有用(它得到订单交易的数量减去取消的订单);

$current_user = wp_get_current_user();
$numorders = wc_get_customer_order_count( $current_user->ID );
// Get CANCELLED orders for customer
$args = array(
    'customer_id' => $current_user->ID,
    'post_status' => 'cancelled',
    'post_type' => 'shop_order',
    'return' => 'ids',
);
$numorders_cancelled = 0;
$numorders_cancelled = count( wc_get_orders( $args ) ); // count the array of orders

// NON-CANCELLED equals TOTAL minus CANCELLED
$num_not_cancelled = $numorders - $numorders_cancelled;

如果您打算同时显示已完成和未完成的订单,您将使用上述代码的前两行,即;

$current_user = wp_get_current_user();
$numorders = wc_get_customer_order_count( $current_user->ID );

经过测试并正在努力;
WP = v4.9.9
WC = v3.5.3

【讨论】:

    【解决方案3】:

    找到了方法。

    $args = [
        'author' => 'id',
        'post_status' => 'any',
        'post_type' => 'shop_order'
    ];
    
    $query = new WP_Query($args);
    
    $orderCountByCustomer = $query->found_posts;
    

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多