From 35d2161fa96c727bbaa334bced05fa6839df8c8d Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 15 Feb 2019 13:05:46 +0000 Subject: [PATCH] Stock lookup --- includes/class-wc-install.php | 1 - .../class-wc-product-data-store-cpt.php | 6 ++-- includes/wc-product-functions.php | 35 +++++++++++++------ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/includes/class-wc-install.php b/includes/class-wc-install.php index 3329ca51100..3ec9518987f 100644 --- a/includes/class-wc-install.php +++ b/includes/class-wc-install.php @@ -827,7 +827,6 @@ CREATE TABLE {$wpdb->prefix}wc_download_log ( ) $collate; CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup ( `product_id` bigint(20) NOT NULL, - `price` decimal(10,{$price_decimals}) NULL default NULL, `min_price` decimal(10,{$price_decimals}) NULL default NULL, `max_price` decimal(10,{$price_decimals}) NULL default NULL, `average_rating` decimal(10,2) NULL default 0, diff --git a/includes/data-stores/class-wc-product-data-store-cpt.php b/includes/data-stores/class-wc-product-data-store-cpt.php index e47dc91eab0..c0e35e47b7a 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -1856,14 +1856,16 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da */ protected function get_data_for_lookup_table( $id, $table ) { if ( 'wc_product_meta_lookup' === $table ) { - $price_meta = (array) get_post_meta( $id, '_price', false ); + $price_meta = (array) get_post_meta( $id, '_price', false ); + $manage_stock = get_post_meta( $id, '_manage_stock', true ); + $stock = 'yes' === $manage_stock ? wc_stock_amount( get_post_meta( $id, '_manage_stock', true ) ) : null; return array( 'product_id' => absint( $id ), - 'price' => reset( $price_meta ), 'min_price' => reset( $price_meta ), 'max_price' => end( $price_meta ), 'average_rating' => get_post_meta( $id, '_wc_average_rating', true ), 'total_sales' => get_post_meta( $id, 'total_sales', true ), + 'stock' => $stock, ); } return array(); diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 7e138554703..844eef09334 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -1275,18 +1275,31 @@ function wc_update_product_lookup_tables() { $result = $wpdb->query( " - INSERT IGNORE INTO {$wpdb->wc_product_meta_lookup} (`product_id`, `price`, `min_price`, `max_price`, `average_rating`, `total_sales`) - SELECT posts.ID, MIN(meta1.meta_value), MIN(meta1.meta_value), MAX(meta1.meta_value), meta2.meta_value, meta3.meta_value + INSERT IGNORE INTO {$wpdb->wc_product_meta_lookup} (`product_id`, `min_price`, `max_price`, `average_rating`, `total_sales`) + SELECT + posts.ID, MIN(meta1.meta_value), MAX(meta1.meta_value), meta2.meta_value, meta3.meta_value FROM {$wpdb->posts} posts - LEFT JOIN {$wpdb->postmeta} meta1 ON posts.ID = meta1.post_id - LEFT JOIN {$wpdb->postmeta} meta2 ON posts.ID = meta2.post_id - LEFT JOIN {$wpdb->postmeta} meta3 ON posts.ID = meta3.post_id - WHERE posts.post_type = 'product' - AND meta1.meta_key = '_price' - AND meta1.meta_value <> '' - AND meta2.meta_key = '_wc_average_rating' - AND meta3.meta_key = 'total_sales' - GROUP BY posts.ID + LEFT JOIN {$wpdb->postmeta} meta1 ON posts.ID = meta1.post_id AND meta1.meta_key = '_price' + LEFT JOIN {$wpdb->postmeta} meta2 ON posts.ID = meta2.post_id AND meta2.meta_key = '_wc_average_rating' + LEFT JOIN {$wpdb->postmeta} meta3 ON posts.ID = meta3.post_id AND meta3.meta_key = 'total_sales' + WHERE + posts.post_type = 'product' + AND meta1.meta_value <> '' + GROUP BY + posts.ID + " + ); + + $result = $wpdb->query( + " + UPDATE + {$wpdb->wc_product_meta_lookup} lookup_table + LEFT JOIN {$wpdb->postmeta} meta1 ON lookup_table.product_id = meta1.post_id AND meta1.meta_key = '_manage_stock' + LEFT JOIN {$wpdb->postmeta} meta2 ON lookup_table.product_id = meta2.post_id AND meta2.meta_key = '_stock' + SET + lookup_table.stock = meta2.meta_value + WHERE + meta1.meta_value = 'yes' " ); }