Php 如何在woocommerce中按日期获取用户订单?
我正在寻找一种标准的方法来获取某个日期范围内或当月的用户订单总数 在探索了woocommerce的源代码之后,我得到的是,woo正在使用类似的东西Php 如何在woocommerce中按日期获取用户订单?,php,wordpress,plugins,woocommerce,Php,Wordpress,Plugins,Woocommerce,我正在寻找一种标准的方法来获取某个日期范围内或当月的用户订单总数 在探索了woocommerce的源代码之后,我得到的是,woo正在使用类似的东西 $order_item_amounts = $this->get_order_report_data( array( 'data' => array( '_line_total' => array( 'type'
$order_item_amounts = $this->get_order_report_data( array(
'data' => array(
'_line_total' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'order_item_amount'
),
'post_date' => array(
'type' => 'post_data',
'function' => '',
'name' => 'post_date'
),
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
),
'where_meta' => array(
'relation' => 'OR',
array(
'type' => 'order_item_meta',
'meta_key' => array( '_product_id', '_variation_id' ),
'meta_value' => $this->product_ids,
'operator' => 'IN'
),
),
'group_by' => 'product_id, ' . $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
) );
在class-wc-report-sales-by-product.php中
但正如你所知,这是基于产品而不是用户。
根据上述代码,
$this->group\u by\u query
部分包含可在woo source中找到的日期条件。我的问题实际上是关于如何使用woocommerce的内置函数根据给定的日期范围生成订单列表
谢谢,我相信这是正确的
您只需要在字段中添加。。。大概是这样的:
public function get_customer_total_order() {
$customer_orders = get_posts( array(
'numberposts' => - 1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => array( 'shop_order' ),
'post_status' => array( 'wc-completed' ),
'date_query' => array(
'after' => date('Y-m-d', strtotime('-10 days')),
'before' => date('Y-m-d', strtotime('today'))
)
) );
$total = 0;
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order );
$total += $order->get_total();
}
return $total;
}
其他阅读资料:
与如何使用get\u order\u report\u data
相关的问题,您可以这样做。。。您可以将其粘贴到要测试的主题上的functions.php
中
include_once( WP_PLUGIN_DIR . '/woocommerce/includes/admin/reports/class-wc-admin-report.php');
$reports = new WC_Admin_Report();
$args = array(
'data' => array(
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_sales'
),
),
'where' => array(
array(
'key' => 'post_date',
'value' => date( 'Y-m-d', strtotime( '01/01/2016' ) ), // starting date
'operator' => '>'
),
array(
'key' => 'post_date',
'value' => date( 'Y-m-d', strtotime( '02/01/2016' ) ), // end date...
'operator' => '<'
),
),
'where_meta' => array(
array(
'meta_key' => '_customer_user',
'meta_value' => '1', // customer id
'operator' => '='
)
),
);
$data = $reports->get_order_report_data($args);
print_r($data); // prints like: stdClass Object ( [total_sales] => 200 )
include_once(WP_PLUGIN_DIR./woocommerce/includes/admin/reports/class wc admin report.php);
$reports=new WC_Admin_Report();
$args=数组(
“数据”=>数组(
“\u顺序\u总数”=>数组(
'type'=>'meta',
'函数'=>'求和',
“名称”=>“总销售额”
),
),
'其中'=>数组(
排列(
'key'=>'post_date',
'值'=>日期('Y-m-d',标准时间('01/01/2016'),//开始日期
'运算符'=>'>'
),
排列(
'key'=>'post_date',
'值'=>日期('Y-m-d',标准时间('02/01/2016'),//结束日期。。。
“operator'=>”谢谢你的回答,它是正确的。实际上,我在几分钟前想起Wordpress date\u查询时实现了这种方法。这是一个好方法。但是有没有其他方法只需一次查询即可完成所有操作,比如get\u order\u report\u data()