diff --git a/includes/class-wc-product-grouped.php b/includes/class-wc-product-grouped.php index 45fb9ce2350..dadf9fe2caa 100644 --- a/includes/class-wc-product-grouped.php +++ b/includes/class-wc-product-grouped.php @@ -52,7 +52,15 @@ class WC_Product_Grouped extends WC_Product { public function is_on_sale( $context = 'view' ) { global $wpdb; - $on_sale = $this->get_children() && null !== $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sale_price' AND meta_value > 0 AND post_id IN (" . implode( ',', array_map( 'esc_sql', $this->get_children() ) ) . ");" ); + $children = array_filter( array_map( 'wc_get_product', $this->get_children( $context ) ), 'wc_products_array_filter_visible_grouped' ); + $on_sale = false; + + foreach ( $children as $child ) { + if ( $child->is_on_sale() ) { + $on_sale = true; + break; + } + } return 'view' === $context ? apply_filters( 'woocommerce_product_is_on_sale', $on_sale, $this ) : $on_sale; } @@ -76,10 +84,10 @@ class WC_Product_Grouped extends WC_Product { public function get_price_html( $price = '' ) { $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); $child_prices = array(); + $children = array_filter( array_map( 'wc_get_product', $this->get_children() ), 'wc_products_array_filter_visible_grouped' ); - foreach ( $this->get_children() as $child_id ) { - $child = wc_get_product( $child_id ); - if ( $child && '' !== $child->get_price() ) { + foreach ( $children as $child ) { + if ( '' !== $child->get_price() ) { $child_prices[] = 'incl' === $tax_display_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child ); } } diff --git a/includes/wc-product-functions.php b/includes/wc-product-functions.php index 5ca0cc8c854..fd9b973c7d6 100644 --- a/includes/wc-product-functions.php +++ b/includes/wc-product-functions.php @@ -1033,6 +1033,17 @@ function wc_products_array_filter_visible( $product ) { return $product && is_a( $product, 'WC_Product' ) && $product->is_visible(); } +/** + * Callback for array filter to get visible grouped products only. + * + * @since 3.1.0 + * @param WC_Product $product + * @return bool + */ +function wc_products_array_filter_visible_grouped( $product ) { + return $product && is_a( $product, 'WC_Product' ) && ( 'publish' === $product->get_status() || current_user_can( 'edit_product', $product->get_id() ) ); +} + /** * Callback for array filter to get products the user can edit only. * diff --git a/includes/wc-template-functions.php b/includes/wc-template-functions.php index 56d04176956..612f04aa428 100644 --- a/includes/wc-template-functions.php +++ b/includes/wc-template-functions.php @@ -1003,7 +1003,7 @@ if ( ! function_exists( 'woocommerce_grouped_add_to_cart' ) ) { function woocommerce_grouped_add_to_cart() { global $product; - $products = array_filter( array_map( 'wc_get_product', $product->get_children() ) ); + $products = array_filter( array_map( 'wc_get_product', $product->get_children() ), 'wc_products_array_filter_visible_grouped' ); if ( $products ) { usort( $products, 'wc_products_array_orderby_menu_order' );