Merge pull request #23019 from woocommerce/update/onsale-lookup

onsale lookup table
This commit is contained in:
Mike Jolley 2019-03-14 11:10:35 +00:00 committed by GitHub
commit 520c517410
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 28 deletions

View File

@ -9,10 +9,8 @@ defined( 'ABSPATH' ) || exit;
?> ?>
<div id="message" class="updated woocommerce-message"> <div id="message" class="updated woocommerce-message">
<a class="woocommerce-message-close notice-dismiss" href="<?php echo esc_url( admin_url( 'admin.php?page=wc-status&tab=action-scheduler&s=wc_update_product_lookup_tables&status=pending' ) ); ?>"><?php esc_html_e( 'View progress', 'woocommerce' ); ?></a>
<p> <p>
<string><?php esc_html_e( 'WooCommerce is updating product data in the background. ', 'woocommerce' ); ?></strong> <string><?php esc_html_e( 'WooCommerce is updating product data in the background.', 'woocommerce' ); ?></strong>&nbsp;
<?php <?php
echo wp_kses_post( echo wp_kses_post(
sprintf( sprintf(
@ -23,5 +21,6 @@ defined( 'ABSPATH' ) || exit;
) )
); );
?> ?>
&nbsp;<a href="<?php echo esc_url( admin_url( 'admin.php?page=wc-status&tab=action-scheduler&s=wc_update_product_lookup_tables&status=pending' ) ); ?>"><?php esc_html_e( 'View progress &rarr;', 'woocommerce' ); ?></a>
</p> </p>
</div> </div>

View File

@ -853,6 +853,7 @@ CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
`downloadable` tinyint(1) NULL default 0, `downloadable` tinyint(1) NULL default 0,
`min_price` decimal(10,2) NULL default NULL, `min_price` decimal(10,2) NULL default NULL,
`max_price` decimal(10,2) NULL default NULL, `max_price` decimal(10,2) NULL default NULL,
`onsale` tinyint(1) NULL default 0,
`stock_quantity` double NULL default NULL, `stock_quantity` double NULL default NULL,
`stock_status` varchar(100) NULL default 'instock', `stock_status` varchar(100) NULL default 'instock',
`rating_count` bigint(20) NULL default 0, `rating_count` bigint(20) NULL default 0,
@ -863,6 +864,7 @@ CREATE TABLE {$wpdb->prefix}wc_product_meta_lookup (
KEY `downloadable` (`downloadable`), KEY `downloadable` (`downloadable`),
KEY `stock_status` (`stock_status`), KEY `stock_status` (`stock_status`),
KEY `stock_quantity` (`stock_quantity`), KEY `stock_quantity` (`stock_quantity`),
KEY `onsale` (`onsale`),
KEY min_max_price (`min_price`, `max_price`) KEY min_max_price (`min_price`, `max_price`)
) $collate; ) $collate;
"; ";

View File

@ -849,7 +849,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
public function get_on_sale_products() { public function get_on_sale_products() {
global $wpdb; global $wpdb;
$decimals = absint( wc_get_price_decimals() );
$exclude_term_ids = array(); $exclude_term_ids = array();
$outofstock_join = ''; $outofstock_join = '';
$outofstock_where = ''; $outofstock_where = '';
@ -867,36 +866,32 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// Fetch a list of non-published parent products and exlude them, quicker than joining in the main query below. // Fetch a list of non-published parent products and exlude them, quicker than joining in the main query below.
$non_published_products = $wpdb->get_col( $non_published_products = $wpdb->get_col(
"SELECT post.ID as id FROM `$wpdb->posts` AS post "
WHERE post.post_type = 'product' SELECT posts.ID as id FROM `$wpdb->posts` AS posts
AND post.post_parent = 0 WHERE posts.post_type = 'product'
AND post.post_status != 'publish'" AND posts.post_parent = 0
AND posts.post_status != 'publish'
"
); );
if ( 0 < count( $non_published_products ) ) { if ( 0 < count( $non_published_products ) ) {
$non_published_where = ' AND post.post_parent NOT IN ( ' . implode( ',', $non_published_products ) . ')'; $non_published_where = ' AND posts.post_parent NOT IN ( ' . implode( ',', $non_published_products ) . ')';
} }
return $wpdb->get_results( return $wpdb->get_results(
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
$wpdb->prepare( "
"SELECT post.ID as id, post.post_parent as parent_id FROM `$wpdb->posts` AS post SELECT posts.ID as id, posts.post_parent as parent_id
LEFT JOIN `$wpdb->postmeta` AS meta ON post.ID = meta.post_id FROM {$wpdb->posts} AS posts
LEFT JOIN `$wpdb->postmeta` AS meta2 ON post.ID = meta2.post_id INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id
$outofstock_join $outofstock_join
WHERE post.post_type IN ( 'product', 'product_variation' ) WHERE posts.post_type IN ( 'product', 'product_variation' )
AND post.post_status = 'publish' AND posts.post_status = 'publish'
AND meta.meta_key = '_sale_price' AND lookup.onsale = 1
AND meta2.meta_key = '_price' $outofstock_where
AND CAST( meta.meta_value AS DECIMAL ) >= 0 $non_published_where
AND CAST( meta.meta_value AS CHAR ) != '' GROUP BY posts.ID
AND CAST( meta.meta_value AS DECIMAL( 10, %d ) ) = CAST( meta2.meta_value AS DECIMAL( 10, %d ) ) "
$outofstock_where // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
$non_published_where
GROUP BY post.ID",
$decimals,
$decimals
)
// phpcs:enable
); );
} }
@ -1890,6 +1885,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$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 ); $manage_stock = get_post_meta( $id, '_manage_stock', true );
$stock = 'yes' === $manage_stock ? wc_stock_amount( get_post_meta( $id, '_stock', true ) ) : null; $stock = 'yes' === $manage_stock ? wc_stock_amount( get_post_meta( $id, '_stock', true ) ) : null;
$price = wc_format_decimal( get_post_meta( $id, '_price', true ) );
$sale_price = wc_format_decimal( get_post_meta( $id, '_sale_price', true ) );
return array( return array(
'product_id' => absint( $id ), 'product_id' => absint( $id ),
'sku' => get_post_meta( $id, '_sku', true ), 'sku' => get_post_meta( $id, '_sku', true ),
@ -1897,6 +1894,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'downloadable' => 'yes' === get_post_meta( $id, '_downloadable', true ) ? 1 : 0, 'downloadable' => 'yes' === get_post_meta( $id, '_downloadable', true ) ? 1 : 0,
'min_price' => reset( $price_meta ), 'min_price' => reset( $price_meta ),
'max_price' => end( $price_meta ), 'max_price' => end( $price_meta ),
'onsale' => $sale_price && $price === $sale_price ? 1 : 0,
'stock_quantity' => $stock, 'stock_quantity' => $stock,
'stock_status' => get_post_meta( $id, '_stock_status', true ), 'stock_status' => get_post_meta( $id, '_stock_status', true ),
'rating_count' => array_sum( (array) get_post_meta( $id, '_wc_rating_count', true ) ), 'rating_count' => array_sum( (array) get_post_meta( $id, '_wc_rating_count', true ) ),

View File

@ -1319,6 +1319,7 @@ function wc_update_product_lookup_tables() {
'total_sales', 'total_sales',
'downloadable', 'downloadable',
'virtual', 'virtual',
'onsale',
); );
foreach ( $columns as $index => $column ) { foreach ( $columns as $index => $column ) {
@ -1442,6 +1443,16 @@ function wc_update_product_lookup_tables_column( $column ) {
case 'downloadable': case 'downloadable':
case 'virtual': case 'virtual':
$column = esc_sql( $column ); $column = esc_sql( $column );
$wpdb->query(
"
UPDATE
{$wpdb->wc_product_meta_lookup} lookup_table
SET
lookup_table.`{$column}` = 0
"
);
$wpdb->query( $wpdb->query(
" "
UPDATE UPDATE
@ -1454,6 +1465,38 @@ function wc_update_product_lookup_tables_column( $column ) {
" "
); );
break; break;
case 'onsale':
$column = esc_sql( $column );
$decimals = absint( wc_get_price_decimals() );
$wpdb->query(
"
UPDATE
{$wpdb->wc_product_meta_lookup} lookup_table
SET
lookup_table.`{$column}` = 0
"
);
$wpdb->query(
$wpdb->prepare(
"
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 = '_price'
LEFT JOIN {$wpdb->postmeta} meta2 ON lookup_table.product_id = meta2.post_id AND meta2.meta_key = '_sale_price'
SET
lookup_table.`{$column}` = 1
WHERE 1=1
AND CAST( meta1.meta_value AS DECIMAL ) >= 0
AND CAST( meta2.meta_value AS CHAR ) != ''
AND CAST( meta1.meta_value AS DECIMAL( 10, %d ) ) = CAST( meta2.meta_value AS DECIMAL( 10, %d ) )
",
$decimals,
$decimals
)
);
break;
} }
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
} }