From 2aa39507f72c90aa1a09d3ae825fa88ad703c389 Mon Sep 17 00:00:00 2001 From: Gerhard Date: Thu, 1 Nov 2018 12:01:59 +0200 Subject: [PATCH] Do not include products who's parents are not in publish status to be part of the get_on_sale_products results --- .../data-stores/class-wc-product-data-store-cpt.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 1b51964abe8..23d82e8c0c8 100644 --- a/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/includes/data-stores/class-wc-product-data-store-cpt.php @@ -848,6 +848,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $exclude_term_ids = array(); $outofstock_join = ''; $outofstock_where = ''; + $non_published_where = ''; $product_visibility_term_ids = wc_get_product_visibility_term_ids(); if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) { @@ -859,6 +860,17 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da $outofstock_where = ' AND exclude_join.object_id IS NULL'; } + // 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( + "SELECT post.ID as id FROM `$wpdb->posts` AS post + WHERE post.post_type = 'product' + AND post.post_parent = 0 + AND post.post_status != 'publish'" + ); + if ( 0 < count( $non_published_products ) ) { + $non_published_where = ' AND post.post_parent NOT IN ( ' . implode( ',', $non_published_products ) . ')'; + } + return $wpdb->get_results( // phpcs:disable WordPress.WP.PreparedSQL.NotPrepared $wpdb->prepare( @@ -874,6 +886,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da AND CAST( meta.meta_value AS CHAR ) != '' AND CAST( meta.meta_value AS DECIMAL( 10, %d ) ) = CAST( meta2.meta_value AS DECIMAL( 10, %d ) ) $outofstock_where + $non_published_where GROUP BY post.ID", $decimals, $decimals