Merge pull request #15703 from woocommerce/fix/15689

Filter out non-visible products when outputting grouped products + prices
This commit is contained in:
Claudiu Lodromanean 2017-06-20 14:50:50 -07:00 committed by GitHub
commit aab401c8f8
3 changed files with 24 additions and 5 deletions

View File

@ -52,7 +52,15 @@ class WC_Product_Grouped extends WC_Product {
public function is_on_sale( $context = 'view' ) { public function is_on_sale( $context = 'view' ) {
global $wpdb; 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; 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 = '' ) { public function get_price_html( $price = '' ) {
$tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); $tax_display_mode = get_option( 'woocommerce_tax_display_shop' );
$child_prices = array(); $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 ) { foreach ( $children as $child ) {
$child = wc_get_product( $child_id ); if ( '' !== $child->get_price() ) {
if ( $child && '' !== $child->get_price() ) {
$child_prices[] = 'incl' === $tax_display_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child ); $child_prices[] = 'incl' === $tax_display_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child );
} }
} }

View File

@ -1033,6 +1033,17 @@ function wc_products_array_filter_visible( $product ) {
return $product && is_a( $product, 'WC_Product' ) && $product->is_visible(); 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. * Callback for array filter to get products the user can edit only.
* *

View File

@ -1003,7 +1003,7 @@ if ( ! function_exists( 'woocommerce_grouped_add_to_cart' ) ) {
function woocommerce_grouped_add_to_cart() { function woocommerce_grouped_add_to_cart() {
global $product; 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 ) { if ( $products ) {
usort( $products, 'wc_products_array_orderby_menu_order' ); usort( $products, 'wc_products_array_orderby_menu_order' );