Filter by variation on product report #3281

This commit is contained in:
Mike Jolley 2013-07-18 14:04:18 +01:00
parent 8104048ddc
commit 1003121870
2 changed files with 80 additions and 11 deletions

View File

@ -110,6 +110,9 @@ class WC_Admin_Report {
if ( ! empty( $where_meta ) ) {
foreach ( $where_meta as $value ) {
if ( ! is_array( $value ) )
continue;
if ( isset( $value['type'] ) && $value['type'] == 'order_item_meta' ) {
$joins["order_items"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id";
@ -152,7 +155,14 @@ class WC_Admin_Report {
}
if ( ! empty( $where_meta ) ) {
foreach ( $where_meta as $value ) {
$relation = isset( $where_meta['relation'] ) ? $where_meta['relation'] : 'AND';
$query['where'] .= " AND (";
foreach ( $where_meta as $index => $value ) {
if ( ! is_array( $value ) )
continue;
if ( strtolower( $value['operator'] ) == 'in' ) {
if ( is_array( $value['meta_value'] ) )
$value['meta_value'] = implode( "','", $value['meta_value'] );
@ -163,15 +173,20 @@ class WC_Admin_Report {
}
if ( ! empty( $where_value ) ) {
if ( $index > 0 )
$query['where'] .= ' ' . $relation;
if ( isset( $value['type'] ) && $value['type'] == 'order_item_meta' ) {
$query['where'] .= " AND order_item_meta_{$value['meta_key']}.meta_key = '{$value['meta_key']}'";
$query['where'] .= " AND order_item_meta_{$value['meta_key']}.meta_value {$where_value}";
$query['where'] .= " ( order_item_meta_{$value['meta_key']}.meta_key = '{$value['meta_key']}'";
$query['where'] .= " AND order_item_meta_{$value['meta_key']}.meta_value {$where_value} )";
} else {
$query['where'] .= " AND meta_{$value['meta_key']}.meta_key = '{$value['meta_key']}'";
$query['where'] .= " AND meta_{$value['meta_key']}.meta_value {$where_value}";
$query['where'] .= " ( meta_{$value['meta_key']}.meta_key = '{$value['meta_key']}'";
$query['where'] .= " AND meta_{$value['meta_key']}.meta_value {$where_value} )";
}
}
}
$query['where'] .= ")";
}
if ( ! empty( $where ) ) {

View File

@ -33,14 +33,27 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'order_item_amount'
)
),
'_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' => '_product_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
),
array(
'type' => 'order_item_meta',
'meta_key' => '_variation_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
)
),
'query_type' => 'get_var',
@ -53,16 +66,30 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'order_item_count'
)
),
'_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' => '_product_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
),
array(
'type' => 'order_item_meta',
'meta_key' => '_variation_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
)
),
'group_by' => 'product_id',
'query_type' => 'get_var',
'filter_range' => true
) ) );
@ -196,7 +223,8 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
$this->product_ids_titles = array();
foreach ( $this->product_ids as $product_id ) {
$this->product_ids_titles[] = get_the_title( $product_id );
$product = get_product( $product_id );
$this->product_ids_titles[] = $product->get_formatted_name();
}
echo '<p>' . ' <strong>' . implode( ', ', $this->product_ids_titles ) . '</strong></p>';
@ -231,7 +259,7 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
dataType: 'json',
afterTypeDelay: 100,
data: {
action: 'woocommerce_json_search_products',
action: 'woocommerce_json_search_products_and_variations',
security: '<?php echo wp_create_nonce("search-products"); ?>'
}
}, function (data) {
@ -395,16 +423,29 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
'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' => '_product_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
),
array(
'type' => 'order_item_meta',
'meta_key' => '_variation_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
)
),
'group_by' => $this->group_by_query,
'group_by' => 'product_id,' . $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true
@ -423,16 +464,29 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
'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' => '_product_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
),
array(
'type' => 'order_item_meta',
'meta_key' => '_variation_id',
'meta_value' => $this->product_ids,
'operator' => 'IN'
)
),
'group_by' => $this->group_by_query,
'group_by' => 'product_id,' . $this->group_by_query,
'order_by' => 'post_date ASC',
'query_type' => 'get_results',
'filter_range' => true