WC_API_Reports::get_top_sellers_report()
Get the top sellers report
Method of the class: WC_API_Reports{}
Hooks from the method
Return
Array|WP_Error
.
Usage
$WC_API_Reports = new WC_API_Reports(); $WC_API_Reports->get_top_sellers_report( $fields, $filter );
- $fields(string)
- fields to include in response
Default: null - $filter(array)
- date filtering
Default: array()
Changelog
Since 2.1 | Introduced. |
WC_API_Reports::get_top_sellers_report() WC API Reports::get top sellers report code WC 8.6.1
public function get_top_sellers_report( $fields = null, $filter = array() ) { // check user permissions $check = $this->validate_request(); if ( is_wp_error( $check ) ) { return $check; } // set date filtering $this->setup_report( $filter ); $top_sellers = $this->report->get_order_report_data( array( 'data' => array( '_product_id' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'product_id', ), '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'order_item_qty', ), ), 'order_by' => 'order_item_qty DESC', 'group_by' => 'product_id', 'limit' => isset( $filter['limit'] ) ? absint( $filter['limit'] ) : 12, 'query_type' => 'get_results', 'filter_range' => true, ) ); $top_sellers_data = array(); foreach ( $top_sellers as $top_seller ) { $product = wc_get_product( $top_seller->product_id ); if ( $product ) { $top_sellers_data[] = array( 'title' => $product->get_name(), 'product_id' => $top_seller->product_id, 'quantity' => $top_seller->order_item_qty, ); } } return array( 'top_sellers' => apply_filters( 'woocommerce_api_report_response', $top_sellers_data, $this->report, $fields, $this->server ) ); }