Improved handling of JOIN clauses
* Refactored report logic to allow developers to specify the type of JOIN to use when fetching report data. Ref https://github.com/woothemes/woocommerce/issues/7818.
This commit is contained in:
parent
32b0684a93
commit
76a0ef1e9f
|
@ -105,18 +105,24 @@ class WC_Admin_Report {
|
|||
|
||||
foreach ( $data as $key => $value ) {
|
||||
|
||||
$join_type = isset($value['join_type']) ? $value['join_type'] : 'INNER';
|
||||
if ( $value['type'] == 'meta' ) {
|
||||
|
||||
$joins["meta_{$key}"] = "LEFT JOIN {$wpdb->postmeta} AS meta_{$key} ON posts.ID = meta_{$key}.post_id";
|
||||
$joins["meta_{$key}"] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON " .
|
||||
"(posts.ID = meta_{$key}.post_id) " .
|
||||
"AND (meta_{$key}.meta_key = '{$key}')";
|
||||
|
||||
} elseif ( $value['type'] == 'order_item_meta' ) {
|
||||
|
||||
$joins["order_items"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
|
||||
$joins["order_item_meta_{$key}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON order_items.order_item_id = order_item_meta_{$key}.order_item_id";
|
||||
|
||||
$joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON (posts.ID = order_items.order_id)";
|
||||
if ( $value['order_item_type'] ) {
|
||||
$joins["order_items"] .= " AND (order_items.order_item_type = '{$value['order_item_type']}')";
|
||||
}
|
||||
$joins["order_item_meta_{$key}"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON " .
|
||||
"(order_items.order_item_id = order_item_meta_{$key}.order_item_id) " .
|
||||
" AND (order_item_meta_{$key}.meta_key = '{$key}')";
|
||||
} elseif ( $value['type'] == 'order_item' ) {
|
||||
|
||||
$joins["order_items"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
|
||||
$joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -128,17 +134,18 @@ class WC_Admin_Report {
|
|||
if ( ! is_array( $value ) ) {
|
||||
continue;
|
||||
}
|
||||
$join_type = isset($value['join_type']) ? $value['join_type'] : 'INNER';
|
||||
|
||||
$key = is_array( $value['meta_key'] ) ? $value['meta_key'][0] . '_array' : $value['meta_key'];
|
||||
|
||||
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_items.order_id";
|
||||
$joins["order_item_meta_{$key}"] = "LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON order_items.order_item_id = order_item_meta_{$key}.order_item_id";
|
||||
$joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
|
||||
$joins["order_item_meta_{$key}"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_{$key} ON order_items.order_item_id = order_item_meta_{$key}.order_item_id";
|
||||
|
||||
} else {
|
||||
// If we have a where clause for meta, join the postmeta table
|
||||
$joins["meta_{$key}"] = "LEFT JOIN {$wpdb->postmeta} AS meta_{$key} ON posts.ID = meta_{$key}.post_id";
|
||||
$joins["meta_{$key}"] = "{$join_type} JOIN {$wpdb->postmeta} AS meta_{$key} ON posts.ID = meta_{$key}.post_id";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -173,21 +180,6 @@ class WC_Admin_Report {
|
|||
";
|
||||
}
|
||||
|
||||
foreach ( $data as $key => $value ) {
|
||||
|
||||
if ( $value['type'] == 'meta' ) {
|
||||
|
||||
$query['where'] .= " AND meta_{$key}.meta_key = '{$key}'";
|
||||
|
||||
} elseif ( $value['type'] == 'order_item_meta' ) {
|
||||
|
||||
if ( $value['order_item_type'] ) {
|
||||
$query['where'] .= " AND order_items.order_item_type = '{$value['order_item_type']}'";
|
||||
}
|
||||
$query['where'] .= " AND order_item_meta_{$key}.meta_key = '{$key}'";
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $where_meta ) ) {
|
||||
|
||||
|
|
Loading…
Reference in New Issue