Hide stock amount when children are involved

Closes #10172
This commit is contained in:
Mike Jolley 2016-01-27 13:00:50 +00:00
parent 30ee8aaef0
commit f8df682f34
2 changed files with 8 additions and 7 deletions

View File

@ -222,15 +222,17 @@ class WC_Product {
*/ */
public function get_total_stock() { public function get_total_stock() {
if ( empty( $this->total_stock ) ) { if ( empty( $this->total_stock ) ) {
if ( sizeof( $this->get_children() ) > 0 ) {
$this->total_stock = max( 0, $this->get_stock_quantity() ); $this->total_stock = max( 0, $this->get_stock_quantity() );
if ( sizeof( $this->get_children() ) > 0 ) {
foreach ( $this->get_children() as $child_id ) { foreach ( $this->get_children() as $child_id ) {
if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) { if ( 'yes' === get_post_meta( $child_id, '_manage_stock', true ) ) {
$stock = get_post_meta( $child_id, '_stock', true ); $stock = get_post_meta( $child_id, '_stock', true );
$this->total_stock += max( 0, wc_stock_amount( $stock ) ); $this->total_stock += max( 0, wc_stock_amount( $stock ) );
} }
} }
} else {
$this->total_stock = $this->get_stock_quantity();
} }
} }
return wc_stock_amount( $this->total_stock ); return wc_stock_amount( $this->total_stock );

View File

@ -222,7 +222,7 @@ class WC_Admin_Post_Types {
$columns['sku'] = __( 'SKU', 'woocommerce' ); $columns['sku'] = __( 'SKU', 'woocommerce' );
} }
if ( 'yes' == get_option( 'woocommerce_manage_stock' ) ) { if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
$columns['is_in_stock'] = __( 'Stock', 'woocommerce' ); $columns['is_in_stock'] = __( 'Stock', 'woocommerce' );
} }
@ -395,19 +395,18 @@ class WC_Admin_Post_Types {
echo '</a>'; echo '</a>';
break; break;
case 'is_in_stock' : case 'is_in_stock' :
if ( $the_product->is_in_stock() ) { if ( $the_product->is_in_stock() ) {
echo '<mark class="instock">' . __( 'In stock', 'woocommerce' ) . '</mark>'; echo '<mark class="instock">' . __( 'In stock', 'woocommerce' ) . '</mark>';
} else { } else {
echo '<mark class="outofstock">' . __( 'Out of stock', 'woocommerce' ) . '</mark>'; echo '<mark class="outofstock">' . __( 'Out of stock', 'woocommerce' ) . '</mark>';
} }
if ( $the_product->managing_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.
echo ' &times; ' . $the_product->get_total_stock(); if ( $the_product->managing_stock() && ! sizeof( $the_product->get_children() ) ) {
echo ' (' . $the_product->get_total_stock() . ')';
} }
break; break;
default : default :
break; break;
} }