Permalink
Browse files

6woo before item + itemmeta

  • Loading branch information...
franciscof5 committed Oct 27, 2018
1 parent f367e80 commit afe3bc68a075a70d3db873037bde09d7ad08f012
@@ -49,9 +49,9 @@ private function get_top_seller() {
$query = array();
$query['fields'] = "SELECT SUM( order_item_meta.meta_value ) as qty, order_item_meta_2.meta_value as product_id
FROM {$wpdb->posts} as posts";
$query['join'] = "INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id ";
$query['join'] .= "INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id ";
$query['join'] .= "INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id ";
$query['join'] = "INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_id ";
$query['join'] .= "INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id ";
$query['join'] .= "INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_itemmeta AS order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id ";
$query['where'] = "WHERE posts.post_type IN ( '" . implode( "','", wc_get_order_types( 'order-count' ) ) . "' ) ";
$query['where'] .= "AND posts.post_status IN ( 'wc-" . implode( "','wc-", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "' ) ";
$query['where'] .= "AND order_item_meta.meta_key = '_qty' ";
@@ -160,18 +160,18 @@ public function get_order_report_data( $args = array() ) {
$joins[ "parent_meta_{$key}" ] = "{$join_type} JOIN {$wpdb->postmeta} AS parent_meta_{$key} ON (posts.post_parent = parent_meta_{$key}.post_id) AND (parent_meta_{$key}.meta_key = '{$raw_key}')";
break;
case 'order_item_meta' :
$joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON (posts.ID = order_items.order_id)";
$joins["order_items"] = "{$join_type} JOIN 6woo_{$wpdb->prefix}woocommerce_order_items AS order_items ON (posts.ID = order_items.order_id)";
if ( ! empty( $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 " .
$joins[ "order_item_meta_{$key}" ] = "{$join_type} JOIN 6woo_{$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 = '{$raw_key}')";
break;
case 'order_item' :
$joins["order_items"] = "{$join_type} JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
$joins["order_items"] = "{$join_type} JOIN 6woo_{$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
break;
}
}
@@ -187,8 +187,8 @@ public function get_order_report_data( $args = array() ) {
if ( 'order_item_meta' === $type ) {
$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";
$joins["order_items"] = "{$join_type} JOIN 6woo_{$wpdb->prefix}woocommerce_order_items AS order_items ON posts.ID = order_items.order_id";
$joins[ "order_item_meta_{$key}" ] = "{$join_type} JOIN 6woo_{$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
@@ -370,8 +370,8 @@ protected function prepare_objects_query( $request ) {
if ( ! empty( $request['product'] ) ) {
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
FROM 6woo_{$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'
", $request['product'] ) );
@@ -64,8 +64,8 @@ public function query_args( $args, $request ) {
if ( ! empty( $request['product'] ) ) {
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
FROM 6woo_{$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'
", $request['product'] ) );
@@ -842,7 +842,7 @@ protected function set_item( $order, $item_type, $item, $action ) {
if ( 'update' === $action ) {
$result = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
$wpdb->prepare( "SELECT * FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
absint( $item['id'] ),
absint( $order->get_id() )
) );
@@ -521,8 +521,8 @@ public function get_product_orders( $id, $fields = null, $filter = array(), $sta
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
FROM 6woo_{$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'
", $id ) );
@@ -888,7 +888,7 @@ protected function set_item( $order, $item_type, $item, $action ) {
if ( 'update' === $action ) {
$result = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
$wpdb->prepare( "SELECT * FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
absint( $item['id'] ),
absint( $order->get_id() )
) );
@@ -580,8 +580,8 @@ public function get_product_orders( $id, $fields = null, $filter = array(), $sta
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
FROM 6woo_{$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'
", $id ) );
@@ -419,8 +419,8 @@ public function query_args( $args, $request ) {
if ( ! empty( $request['product'] ) ) {
$order_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
FROM 6woo_{$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'
", $request['product'] ) );
@@ -769,7 +769,7 @@ protected function set_item( $order, $item_type, $posted ) {
// Verify provided line item ID is associated with order.
if ( 'update' === $action ) {
$result = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
$wpdb->prepare( "SELECT * FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
absint( $posted['id'] ),
absint( $order->get_id() )
) );
@@ -797,9 +797,9 @@ public function check_cart_item_stock() {
$wpdb->prepare( "
SELECT SUM( order_item_meta.meta_value ) AS held_qty
FROM {$wpdb->posts} AS posts
LEFT JOIN {$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id
LEFT JOIN 6woo_{$wpdb->prefix}woocommerce_order_items as order_items ON posts.ID = order_items.order_id
LEFT JOIN 6woo_{$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN 6woo_{$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta2 ON order_items.order_item_id = order_item_meta2.order_item_id
WHERE order_item_meta.meta_key = '_qty'
AND order_item_meta2.meta_key = %s AND order_item_meta2.meta_value = %d
AND posts.post_type IN ( '" . implode( "','", wc_get_order_types() ) . "' )
@@ -602,15 +602,15 @@ private static function get_schema() {
KEY download_order_product (download_id,order_id,product_id),
KEY order_id (order_id)
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_order_items (
CREATE TABLE 6woo_{$wpdb->prefix}woocommerce_order_items (
order_item_id BIGINT UNSIGNED NOT NULL auto_increment,
order_item_name TEXT NOT NULL,
order_item_type varchar(200) NOT NULL DEFAULT '',
order_id BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (order_item_id),
KEY order_id (order_id)
) $collate;
CREATE TABLE {$wpdb->prefix}woocommerce_order_itemmeta (
CREATE TABLE 6woo_{$wpdb->prefix}woocommerce_order_itemmeta (
meta_id BIGINT UNSIGNED NOT NULL auto_increment,
order_item_id BIGINT UNSIGNED NOT NULL,
meta_key varchar(255) default NULL,
@@ -999,8 +999,8 @@ public static function wpmu_drop_tables( $tables ) {
$tables[] = $wpdb->prefix . 'woocommerce_termmeta';
$tables[] = $wpdb->prefix . 'woocommerce_tax_rates';
$tables[] = $wpdb->prefix . 'woocommerce_tax_rate_locations';
$tables[] = $wpdb->prefix . 'woocommerce_order_items';
$tables[] = $wpdb->prefix . 'woocommerce_order_itemmeta';
$tables[] = "6woo_".$wpdb->prefix."woocommerce_order_items";
$tables[] = "6woo_".$wpdb->prefix."woocommerce_order_itemmeta";
$tables[] = $wpdb->prefix . 'woocommerce_payment_tokens';
$tables[] = $wpdb->prefix . 'woocommerce_shipping_zones';
$tables[] = $wpdb->prefix . 'woocommerce_shipping_zone_locations';
@@ -453,10 +453,10 @@ public static function delete_order_items( $postid ) {
do_action( 'woocommerce_delete_order_items', $postid );
$wpdb->query( "
DELETE {$wpdb->prefix}woocommerce_order_items, {$wpdb->prefix}woocommerce_order_itemmeta
FROM {$wpdb->prefix}woocommerce_order_items
JOIN {$wpdb->prefix}woocommerce_order_itemmeta ON {$wpdb->prefix}woocommerce_order_items.order_item_id = {$wpdb->prefix}woocommerce_order_itemmeta.order_item_id
WHERE {$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}';
DELETE 6woo_{$wpdb->prefix}woocommerce_order_items, 6woo_{$wpdb->prefix}woocommerce_order_itemmeta
FROM 6woo_{$wpdb->prefix}woocommerce_order_items
JOIN 6woo_{$wpdb->prefix}woocommerce_order_itemmeta ON 6woo_{$wpdb->prefix}woocommerce_order_items.order_item_id = 6woo_{$wpdb->prefix}woocommerce_order_itemmeta.order_item_id
WHERE 6woo_{$wpdb->prefix}woocommerce_order_items.order_id = '{$postid}';
" );
do_action( 'woocommerce_deleted_order_items', $postid );
@@ -636,7 +636,7 @@ private function load_webhooks() {
public function wpdb_table_fix() {
global $wpdb;
$wpdb->payment_tokenmeta = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
$wpdb->order_itemmeta = $wpdb->prefix . 'woocommerce_order_itemmeta';
$wpdb->order_itemmeta = "6woo_".$wpdb->prefix."woocommerce_order_itemmeta";
$wpdb->tables[] = 'woocommerce_payment_tokenmeta';
$wpdb->tables[] = 'woocommerce_order_itemmeta';
@@ -326,7 +326,7 @@ public function read_items( $order, $type ) {
if ( false === $items ) {
$items = $wpdb->get_results(
$wpdb->prepare( "SELECT order_item_type, order_item_id, order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d ORDER BY order_item_id;", $order->get_id() )
$wpdb->prepare( "SELECT order_item_type, order_item_id, order_id, order_item_name FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_id = %d ORDER BY order_item_id;", $order->get_id() )
);
foreach ( $items as $item ) {
wp_cache_set( 'item-' . $item->order_item_id, $item, 'order-items' );
@@ -354,11 +354,11 @@ public function read_items( $order, $type ) {
public function delete_items( $order, $type = null ) {
global $wpdb;
if ( ! empty( $type ) ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $order->get_id(), $type ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $order->get_id(), $type ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING 6woo_{$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id AND items.order_id = %d AND items.order_item_type = %s", $order->get_id(), $type ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_id = %d AND order_item_type = %s", $order->get_id(), $type ) );
} else {
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING {$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN {$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM itemmeta USING 6woo_{$wpdb->prefix}woocommerce_order_itemmeta itemmeta INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_items items WHERE itemmeta.order_item_id = items.order_item_id and items.order_id = %d", $order->get_id() ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_id = %d", $order->get_id() ) );
}
$this->clear_caches( $order );
}
@@ -36,7 +36,7 @@
public function create( &$item ) {
global $wpdb;
$wpdb->insert( $wpdb->prefix . 'woocommerce_order_items', array(
$wpdb->insert( "6woo_".$wpdb->prefix."woocommerce_order_items", array(
'order_item_name' => $item->get_name(),
'order_item_type' => $item->get_type(),
'order_id' => $item->get_order_id(),
@@ -62,7 +62,7 @@ public function update( &$item ) {
$changes = $item->get_changes();
if ( array_intersect( array( 'name', 'order_id' ), array_keys( $changes ) ) ) {
$wpdb->update( $wpdb->prefix . 'woocommerce_order_items', array(
$wpdb->update( "6woo_".$wpdb->prefix."woocommerce_order_items", array(
'order_item_name' => $item->get_name(),
'order_item_type' => $item->get_type(),
'order_id' => $item->get_order_id(),
@@ -88,8 +88,8 @@ public function delete( &$item, $args = array() ) {
if ( $item->get_id() ) {
global $wpdb;
do_action( 'woocommerce_before_delete_order_item', $item->get_id() );
$wpdb->delete( $wpdb->prefix . 'woocommerce_order_items', array( 'order_item_id' => $item->get_id() ) );
$wpdb->delete( $wpdb->prefix . 'woocommerce_order_itemmeta', array( 'order_item_id' => $item->get_id() ) );
$wpdb->delete( "6woo_".$wpdb->prefix."woocommerce_order_items", array( 'order_item_id' => $item->get_id() ) );
$wpdb->delete( "6woo_".$wpdb->prefix."woocommerce_order_itemmeta", array( 'order_item_id' => $item->get_id() ) );
do_action( 'woocommerce_delete_order_item', $item->get_id() );
$this->clear_cache( $item );
}
@@ -113,7 +113,7 @@ public function read( &$item ) {
$data = wp_cache_get( 'item-' . $item->get_id(), 'order-items' );
if ( false === $data ) {
$data = $wpdb->get_row( $wpdb->prepare( "SELECT order_id, order_item_name FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) );
$data = $wpdb->get_row( $wpdb->prepare( "SELECT order_id, order_item_name FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item->get_id() ) );
wp_cache_set( 'item-' . $item->get_id(), $data, 'order-items' );
}
@@ -314,9 +314,9 @@ public function get_total_tax_refunded( $order ) {
$total = $wpdb->get_var(
$wpdb->prepare(
"SELECT SUM( order_itemmeta.meta_value )
FROM {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'tax' )
INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'tax' )
WHERE order_itemmeta.order_item_id = order_items.order_item_id
AND order_itemmeta.meta_key IN ('tax_amount', 'shipping_tax_amount')",
$order->get_id()
@@ -338,9 +338,9 @@ public function get_total_shipping_refunded( $order ) {
$total = $wpdb->get_var(
$wpdb->prepare(
"SELECT SUM( order_itemmeta.meta_value )
FROM {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
FROM 6woo_{$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta
INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d )
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'shipping' )
INNER JOIN 6woo_{$wpdb->prefix}woocommerce_order_items AS order_items ON ( order_items.order_id = posts.ID AND order_items.order_item_type = 'shipping' )
WHERE order_itemmeta.order_item_id = order_items.order_item_id
AND order_itemmeta.meta_key IN ('cost')",
$order->get_id()
@@ -507,7 +507,7 @@ public function search_orders( $term ) {
$wpdb->get_col(
$wpdb->prepare(
"SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items as order_items
FROM 6woo_{$wpdb->prefix}woocommerce_order_items as order_items
WHERE order_item_name LIKE %s",
'%' . $wpdb->esc_like( wc_clean( $term ) ) . '%'
)
@@ -751,6 +751,6 @@ public function query( $query_vars ) {
*/
public function get_order_item_type( $order, $order_item_id ) {
global $wpdb;
return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT order_item_type FROM {$wpdb->prefix}woocommerce_order_items WHERE order_id = %d and order_item_id = %d;", $order->get_id(), $order_item_id ) );
return $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT order_item_type FROM 6woo_{$wpdb->prefix}woocommerce_order_items WHERE order_id = %d and order_item_id = %d;", $order->get_id(), $order_item_id ) );
}
}
Oops, something went wrong.

0 comments on commit afe3bc6

Please sign in to comment.