From 3ca70b30f564e3b92f00054c725c2823fd884113 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 24 Jun 2014 13:01:34 +0100 Subject: [PATCH] Add sanity check to stock status change --- includes/abstracts/abstract-wc-product.php | 40 +++++++--------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/includes/abstracts/abstract-wc-product.php b/includes/abstracts/abstract-wc-product.php index cbd4d7d846f..1d49c32fc24 100644 --- a/includes/abstracts/abstract-wc-product.php +++ b/includes/abstracts/abstract-wc-product.php @@ -152,7 +152,7 @@ class WC_Product { /** * Check if the stock status needs changing */ - private function check_stock_status() { + protected function check_stock_status() { // Update stock status if ( ! $this->backorders_allowed() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { $this->set_stock_status( 'outofstock' ); @@ -193,9 +193,6 @@ class WC_Product { // Clear caches wp_cache_delete( $this->id, 'post_meta' ); - // Clear total stock transient - delete_transient( 'wc_product_total_stock_' . $this->id ); - // Stock status $this->check_stock_status(); @@ -235,6 +232,13 @@ class WC_Product { public function set_stock_status( $status ) { $status = ( 'outofstock' === $status ) ? 'outofstock' : 'instock'; + // Sanity check + if ( $this->managing_stock() ) { + if ( ! $this->backorders_allowed() && $this->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { + $status = 'outofstock'; + } + } + if ( update_post_meta( $this->id, '_stock_status', $status ) ) { do_action( 'woocommerce_product_set_stock_status', $this->id, $status ); } @@ -502,30 +506,12 @@ class WC_Product { * @return bool */ public function is_in_stock() { - if ( $this->managing_stock() ) { - - if ( $this->backorders_allowed() ) { - return true; - } else { - if ( $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { - return false; - } else { - if ( $this->stock_status === 'instock' ) { - return true; - } else { - return false; - } - } - } - + if ( $this->managing_stock() && $this->backorders_allowed() ) { + return true; + } elseif ( $this->managing_stock() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { + return false; } else { - - if ( $this->stock_status === 'instock' ) { - return true; - } else { - return false; - } - + return $this->stock_status === 'instock'; } }