Stock lookup
This commit is contained in:
parent
3309276e82
commit
35d2161fa9
|
@ -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,
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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'
|
||||
"
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue