Stock lookup

This commit is contained in:
Mike Jolley 2019-02-15 13:05:46 +00:00
parent 3309276e82
commit 35d2161fa9
3 changed files with 28 additions and 14 deletions

View File

@ -827,7 +827,6 @@ CREATE TABLE {$wpdb->prefix}wc_download_log (
) $collate; ) $collate;
CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup ( CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
`product_id` bigint(20) NOT NULL, `product_id` bigint(20) NOT NULL,
`price` decimal(10,{$price_decimals}) NULL default NULL,
`min_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, `max_price` decimal(10,{$price_decimals}) NULL default NULL,
`average_rating` decimal(10,2) NULL default 0, `average_rating` decimal(10,2) NULL default 0,

View File

@ -1857,13 +1857,15 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
protected function get_data_for_lookup_table( $id, $table ) { protected function get_data_for_lookup_table( $id, $table ) {
if ( 'wc_product_meta_lookup' === $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( return array(
'product_id' => absint( $id ), 'product_id' => absint( $id ),
'price' => reset( $price_meta ),
'min_price' => reset( $price_meta ), 'min_price' => reset( $price_meta ),
'max_price' => end( $price_meta ), 'max_price' => end( $price_meta ),
'average_rating' => get_post_meta( $id, '_wc_average_rating', true ), 'average_rating' => get_post_meta( $id, '_wc_average_rating', true ),
'total_sales' => get_post_meta( $id, 'total_sales', true ), 'total_sales' => get_post_meta( $id, 'total_sales', true ),
'stock' => $stock,
); );
} }
return array(); return array();

View File

@ -1275,18 +1275,31 @@ function wc_update_product_lookup_tables() {
$result = $wpdb->query( $result = $wpdb->query(
" "
INSERT IGNORE INTO {$wpdb->wc_product_meta_lookup} (`product_id`, `price`, `min_price`, `max_price`, `average_rating`, `total_sales`) 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), MIN(meta1.meta_value), MAX(meta1.meta_value), meta2.meta_value, meta3.meta_value SELECT
posts.ID, MIN(meta1.meta_value), MAX(meta1.meta_value), meta2.meta_value, meta3.meta_value
FROM {$wpdb->posts} posts FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->postmeta} meta1 ON posts.ID = meta1.post_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 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 LEFT JOIN {$wpdb->postmeta} meta3 ON posts.ID = meta3.post_id AND meta3.meta_key = 'total_sales'
WHERE posts.post_type = 'product' WHERE
AND meta1.meta_key = '_price' posts.post_type = 'product'
AND meta1.meta_value <> '' AND meta1.meta_value <> ''
AND meta2.meta_key = '_wc_average_rating' GROUP BY
AND meta3.meta_key = 'total_sales' posts.ID
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'
" "
); );
} }