From f8df682f34b7627d89954d92740e514127a6582c Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 27 Jan 2016 13:00:50 +0000 Subject: [PATCH] Hide stock amount when children are involved Closes #10172 --- includes/abstracts/abstract-wc-product.php | 6 ++++-- includes/admin/class-wc-admin-post-types.php | 9 ++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index 2354b12fbfd..52c032dd021 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -222,15 +222,17 @@ class WC_Product { */ public function get_total_stock() { if ( empty( $this->total_stock ) ) { - $this->total_stock = max( 0, $this->get_stock_quantity() ); - if ( sizeof( $this->get_children() ) > 0 ) { + $this->total_stock = max( 0, $this->get_stock_quantity() ); + foreach ( $this->get_children() as $child_id ) { if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) { $stock = get_post_meta( $child_id, '_stock', true ); $this->total_stock += max( 0, wc_stock_amount( $stock ) ); } } + } else { + $this->total_stock = $this->get_stock_quantity(); } } return wc_stock_amount( $this->total_stock ); diff --git a/includes/admin/class-wc-admin-post-types.php b/includes/admin/class-wc-admin-post-types.php index 36c22214e6a..d616301ecaa 100644 --- a/includes/admin/class-wc-admin-post-types.php +++ b/includes/admin/class-wc-admin-post-types.php @@ -222,7 +222,7 @@ class WC_Admin_Post_Types { $columns['sku'] = __( 'SKU', 'woocommerce' ); } - if ( 'yes' == get_option( 'woocommerce_manage_stock' ) ) { + if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) { $columns['is_in_stock'] = __( 'Stock', 'woocommerce' ); } @@ -395,19 +395,18 @@ class WC_Admin_Post_Types { echo ''; break; case 'is_in_stock' : - if ( $the_product->is_in_stock() ) { echo '' . __( 'In stock', 'woocommerce' ) . ''; } else { echo '' . __( 'Out of stock', 'woocommerce' ) . ''; } - if ( $the_product->managing_stock() ) { - echo ' × ' . $the_product->get_total_stock(); + // If the product has children, a single stock level would be misleading as some could be -ve and some +ve, some managed/some unmanaged etc so hide stock level in this case. + if ( $the_product->managing_stock() && ! sizeof( $the_product->get_children() ) ) { + echo ' (' . $the_product->get_total_stock() . ')'; } break; - default : break; }