Data example:
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'total_sales'
)
public get_order_report_data ( array $args = [] ) : mixed | ||
$args | array | |
return | mixed | depending on query_type |
function hm_wcst_get_product_sales($productIds, $metaKey, $periodStart, $periodEnd) { global $woocommerce; $salesQuantities = array_combine($productIds, array_fill(0, count($productIds), 0)); include_once $woocommerce->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php'; $wc_report = new WC_Admin_Report(); $wc_report->start_date = $periodStart; $wc_report->end_date = $periodEnd; // Based on woocoommerce/includes/admin/reports/class-wc-report-sales-by-product.php $soldProducts = $wc_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' => 'quantity')), 'query_type' => 'get_results', 'group_by' => 'product_id', 'limit' => '', 'filter_range' => true, 'order_types' => wc_get_order_types('order_count'))); foreach ($soldProducts as $product) { if (isset($salesQuantities[$product->product_id])) { $salesQuantities[$product->product_id] = $product->quantity; } } /* foreach ($salesQuantities as $productId => $quantity) { update_post_meta($productId, $metaKey, $quantity); } */ return $salesQuantities; }
function hm_xoiwc_export_body($dest, $return = false) { global $woocommerce, $wpdb; // Calculate report start and end dates (timestamps) switch ($_POST['report_time']) { case '0d': $end_date = strtotime('midnight', current_time('timestamp')); $start_date = $end_date; break; case '1d': $end_date = strtotime('midnight', current_time('timestamp')) - 86400; $start_date = $end_date; break; case '7d': $end_date = strtotime('midnight', current_time('timestamp')) - 86400; $start_date = $end_date - 86400 * 7; break; case 'custom': $end_date = strtotime('midnight', strtotime($_POST['report_end'])); $start_date = strtotime('midnight', strtotime($_POST['report_start'])); break; default: // 30 days is the default $end_date = strtotime('midnight', current_time('timestamp')) - 86400; $start_date = $end_date - 86400 * 30; } // Assemble order by string $orderby = in_array($_POST['orderby'], array('order_id')) ? $_POST['orderby'] : 'product_id'; $orderby .= ' ' . ($_POST['orderdir'] == 'asc' ? 'ASC' : 'DESC'); // Create a new WC_Admin_Report object include_once $woocommerce->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php'; $wc_report = new WC_Admin_Report(); $wc_report->start_date = $start_date; $wc_report->end_date = $end_date; // Get report data $reportData = array('_product_id' => array('type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'product_id'), 'order_id' => array('type' => 'order_item', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'order_id')); if (in_array('quantity', $_POST['fields'])) { $reportData['_qty'] = array('type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'quantity'); } if (in_array('line_subtotal', $_POST['fields'])) { $reportData['_line_subtotal'] = array('type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'line_subtotal'); } if (in_array('line_total', $_POST['fields'])) { $reportData['_line_total'] = array('type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'line_total'); } if (in_array('order_status', $_POST['fields'])) { $reportData['post_status'] = array('type' => 'post_data', 'name' => 'order_status', 'function' => ''); } if (in_array('order_date', $_POST['fields'])) { $reportData['post_date'] = array('type' => 'post_data', 'name' => 'order_date', 'function' => ''); } if (in_array('billing_name', $_POST['fields'])) { $reportData['_billing_first_name'] = array('type' => 'meta', 'name' => 'billing_first_name', 'join_type' => 'LEFT', 'function' => ''); $reportData['_billing_last_name'] = array('type' => 'meta', 'name' => 'billing_last_name', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('billing_phone', $_POST['fields'])) { $reportData['_billing_phone'] = array('type' => 'meta', 'name' => 'billing_phone', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('billing_email', $_POST['fields'])) { $reportData['_billing_email'] = array('type' => 'meta', 'name' => 'billing_email', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('billing_address', $_POST['fields'])) { $reportData['_billing_address_1'] = array('type' => 'meta', 'name' => 'billing_address_1', 'join_type' => 'LEFT', 'function' => ''); $reportData['_billing_address_2'] = array('type' => 'meta', 'name' => 'billing_address_2', 'join_type' => 'LEFT', 'function' => ''); $reportData['_billing_city'] = array('type' => 'meta', 'name' => 'billing_city', 'join_type' => 'LEFT', 'function' => ''); $reportData['_billing_state'] = array('type' => 'meta', 'name' => 'billing_state', 'join_type' => 'LEFT', 'function' => ''); $reportData['_billing_postcode'] = array('type' => 'meta', 'name' => 'billing_postcode', 'join_type' => 'LEFT', 'function' => ''); $reportData['_billing_country'] = array('type' => 'meta', 'name' => 'billing_country', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('shipping_name', $_POST['fields'])) { $reportData['_shipping_first_name'] = array('type' => 'meta', 'name' => 'shipping_first_name', 'join_type' => 'LEFT', 'function' => ''); $reportData['_shipping_last_name'] = array('type' => 'meta', 'name' => 'shipping_last_name', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('shipping_phone', $_POST['fields'])) { $reportData['_shipping_phone'] = array('type' => 'meta', 'name' => 'shipping_phone', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('shipping_email', $_POST['fields'])) { $reportData['_shipping_email'] = array('type' => 'meta', 'name' => 'shipping_email', 'join_type' => 'LEFT', 'function' => ''); } if (in_array('shipping_address', $_POST['fields'])) { $reportData['_shipping_address_1'] = array('type' => 'meta', 'name' => 'shipping_address_1', 'join_type' => 'LEFT', 'function' => ''); $reportData['_shipping_address_2'] = array('type' => 'meta', 'name' => 'shipping_address_2', 'join_type' => 'LEFT', 'function' => ''); $reportData['_shipping_city'] = array('type' => 'meta', 'name' => 'shipping_city', 'join_type' => 'LEFT', 'function' => ''); $reportData['_shipping_state'] = array('type' => 'meta', 'name' => 'shipping_state', 'join_type' => 'LEFT', 'function' => ''); $reportData['_shipping_postcode'] = array('type' => 'meta', 'name' => 'shipping_postcode', 'join_type' => 'LEFT', 'function' => ''); $reportData['_shipping_country'] = array('type' => 'meta', 'name' => 'shipping_country', 'join_type' => 'LEFT', 'function' => ''); } // Avoid max join size error $wpdb->query('SET SQL_BIG_SELECTS=1'); // Prevent plugins from overriding the order status filter add_filter('woocommerce_reports_order_statuses', 'hm_xoiwc_report_order_statuses', 9999); // Based on woocoommerce/includes/admin/reports/class-wc-report-sales-by-product.php $sold_products = $wc_report->get_order_report_data(array('data' => $reportData, 'query_type' => 'get_results', 'group_by' => '', 'order_by' => $orderby, 'limit' => !empty($_POST['limit_on']) && is_numeric($_POST['limit']) ? $_POST['limit'] : '', 'filter_range' => $_POST['report_time'] != 'all', 'order_types' => wc_get_order_types('order_count'), 'order_status' => hm_xoiwc_report_order_statuses())); // Remove report order statuses filter remove_filter('woocommerce_reports_order_statuses', 'hm_xoiwc_report_order_statuses', 9999); // Output report rows foreach ($sold_products as $product) { $row = array(); foreach ($_POST['fields'] as $field) { switch ($field) { case 'product_id': $row[] = $product->product_id; break; case 'order_id': $row[] = $product->order_id; break; case 'order_status': $row[] = wc_get_order_status_name($product->order_status); break; case 'order_date': $row[] = $product->order_date; break; case 'product_sku': $row[] = get_post_meta($product->product_id, '_sku', true); break; case 'product_name': $row[] = html_entity_decode(get_the_title($product->product_id)); break; case 'product_categories': $terms = get_the_terms($product->product_id, 'product_cat'); if (empty($terms)) { $row[] = ''; } else { $categories = array(); foreach ($terms as $term) { $categories[] = $term->name; } $row[] = implode(', ', $categories); } break; case 'billing_name': $row[] = $product->billing_first_name . ' ' . $product->billing_last_name; break; case 'billing_phone': $row[] = $product->billing_phone; break; case 'billing_email': $row[] = $product->billing_email; break; case 'billing_address': $addressComponents = array(); if (!empty($product->billing_address_1)) { $addressComponents[] = $product->billing_address_1; } if (!empty($product->billing_address_2)) { $addressComponents[] = $product->billing_address_2; } if (!empty($product->billing_city)) { $addressComponents[] = $product->billing_city; } if (!empty($product->billing_state)) { $addressComponents[] = $product->billing_state; } if (!empty($product->billing_postcode)) { $addressComponents[] = $product->billing_postcode; } if (!empty($product->billing_country)) { $addressComponents[] = $product->billing_country; } $row[] = implode(', ', $addressComponents); break; case 'shipping_name': $row[] = $product->shipping_first_name . ' ' . $product->shipping_last_name; break; case 'shipping_phone': $row[] = $product->shipping_phone; break; case 'shipping_email': $row[] = $product->shipping_email; break; case 'shipping_address': $addressComponents = array(); if (!empty($product->shipping_address_1)) { $addressComponents[] = $product->shipping_address_1; } if (!empty($product->shipping_address_2)) { $addressComponents[] = $product->shipping_address_2; } if (!empty($product->shipping_city)) { $addressComponents[] = $product->shipping_city; } if (!empty($product->shipping_state)) { $addressComponents[] = $product->shipping_state; } if (!empty($product->shipping_postcode)) { $addressComponents[] = $product->shipping_postcode; } if (!empty($product->shipping_country)) { $addressComponents[] = $product->shipping_country; } $row[] = implode(', ', $addressComponents); break; case 'quantity': $row[] = $product->quantity; break; case 'line_subtotal': $row[] = $product->line_subtotal; break; case 'line_total': $row[] = $product->line_total; break; } } if ($return) { $rows[] = $row; } else { fputcsv($dest, $row); } } if ($return) { return $rows; } }
function hm_sbp_export_body($dest, $return = false) { global $woocommerce, $wpdb; $product_ids = array(); if ($_POST['products'] == 'cats') { $cats = array(); foreach ($_POST['product_cats'] as $cat) { if (is_numeric($cat)) { $cats[] = $cat; } } $product_ids = get_objects_in_term($cats, 'product_cat'); } else { if ($_POST['products'] == 'ids') { foreach (explode(',', $_POST['product_ids']) as $productId) { $productId = trim($productId); if (is_numeric($productId)) { $product_ids[] = $productId; } } } } // Calculate report start and end dates (timestamps) switch ($_POST['report_time']) { case '0d': $end_date = strtotime('midnight', current_time('timestamp')); $start_date = $end_date; break; case '1d': $end_date = strtotime('midnight', current_time('timestamp')) - 86400; $start_date = $end_date; break; case '7d': $end_date = strtotime('midnight', current_time('timestamp')) - 86400; $start_date = $end_date - 86400 * 7; break; case 'custom': $end_date = strtotime('midnight', strtotime($_POST['report_end'])); $start_date = strtotime('midnight', strtotime($_POST['report_start'])); break; default: // 30 days is the default $end_date = strtotime('midnight', current_time('timestamp')) - 86400; $start_date = $end_date - 86400 * 30; } // Assemble order by string $orderby = in_array($_POST['orderby'], array('product_id', 'gross', 'gross_after_discount')) ? $_POST['orderby'] : 'quantity'; $orderby .= ' ' . ($_POST['orderdir'] == 'asc' ? 'ASC' : 'DESC'); // Create a new WC_Admin_Report object include_once $woocommerce->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php'; $wc_report = new WC_Admin_Report(); $wc_report->start_date = $start_date; $wc_report->end_date = $end_date; $where_meta = array(); if ($_POST['products'] != 'all') { $where_meta[] = array('type' => 'order_item_meta', 'meta_key' => '_product_id', 'operator' => 'in', 'meta_value' => $product_ids); } if (!empty($_POST['exclude_free'])) { $where_meta[] = array('meta_key' => '_line_total', 'meta_value' => 0, 'operator' => '!=', 'type' => 'order_item_meta'); } // Get report data // Avoid max join size error $wpdb->query('SET SQL_BIG_SELECTS=1'); // Prevent plugins from overriding the order status filter add_filter('woocommerce_reports_order_statuses', 'hm_psr_report_order_statuses', 9999); // Based on woocoommerce/includes/admin/reports/class-wc-report-sales-by-product.php $sold_products = $wc_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' => 'quantity'), '_line_subtotal' => array('type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'gross'), '_line_total' => array('type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'gross_after_discount')), 'query_type' => 'get_results', 'group_by' => 'product_id', 'where_meta' => $where_meta, 'order_by' => $orderby, 'limit' => !empty($_POST['limit_on']) && is_numeric($_POST['limit']) ? $_POST['limit'] : '', 'filter_range' => $_POST['report_time'] != 'all', 'order_types' => wc_get_order_types('order_count'), 'order_status' => hm_psr_report_order_statuses())); // Remove report order statuses filter remove_filter('woocommerce_reports_order_statuses', 'hm_psr_report_order_statuses', 9999); if ($return) { $rows = array(); } // Output report rows foreach ($sold_products as $product) { $row = array(); foreach ($_POST['fields'] as $field) { switch ($field) { case 'product_id': $row[] = $product->product_id; break; case 'variation_id': $row[] = empty($product->variation_id) ? '' : $product->variation_id; break; case 'product_sku': $row[] = get_post_meta($product->product_id, '_sku', true); break; case 'product_name': $row[] = html_entity_decode(get_the_title($product->product_id)); break; case 'quantity_sold': $row[] = $product->quantity; break; case 'gross_sales': $row[] = $product->gross; break; case 'gross_after_discount': $row[] = $product->gross_after_discount; break; case 'product_categories': $terms = get_the_terms($product->product_id, 'product_cat'); if (empty($terms)) { $row[] = ''; } else { $categories = array(); foreach ($terms as $term) { $categories[] = $term->name; } $row[] = implode(', ', $categories); } break; } } if ($return) { $rows[] = $row; } else { fputcsv($dest, $row); } } if ($return) { return $rows; } }
/** * Get report totals such as order totals and discount amounts. * IMPORTANT: to get a correct total amount, totals fields should be replaced * by their "_base_currency" counterpart, as follows: * _order_total -> _order_total_base_currency * _order_discount -> _order_discount_base_currency * _cart_discount -> _cart_discount_base_currency * _order_shipping -> _order_shipping_base_currency * * Data example: * * '_order_total_base_currency' => array( * 'type' => 'meta', * 'function' => 'SUM', * 'name' => 'total_sales' * ) * * @param array args The parameters to prepare the report. * @return array|string depending on query_type */ public function get_order_report_data($args = array()) { // Just call parent method. This method exists mainly to allow documenting it, // in order to highlight the importance of using the "*_base_currency" fields return parent::get_order_report_data($args); }