Add sanity check to stock status change

This commit is contained in:
Mike Jolley 2014-06-24 13:01:34 +01:00
parent 63c0b75972
commit 3ca70b30f5
1 changed files with 13 additions and 27 deletions

View File

@ -152,7 +152,7 @@ class WC_Product {
/** /**
* Check if the stock status needs changing * Check if the stock status needs changing
*/ */
private function check_stock_status() { protected function check_stock_status() {
// Update stock status // Update stock status
if ( ! $this->backorders_allowed() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) { if ( ! $this->backorders_allowed() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
$this->set_stock_status( 'outofstock' ); $this->set_stock_status( 'outofstock' );
@ -193,9 +193,6 @@ class WC_Product {
// Clear caches // Clear caches
wp_cache_delete( $this->id, 'post_meta' ); wp_cache_delete( $this->id, 'post_meta' );
// Clear total stock transient
delete_transient( 'wc_product_total_stock_' . $this->id );
// Stock status // Stock status
$this->check_stock_status(); $this->check_stock_status();
@ -235,6 +232,13 @@ class WC_Product {
public function set_stock_status( $status ) { public function set_stock_status( $status ) {
$status = ( 'outofstock' === $status ) ? 'outofstock' : 'instock'; $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 ) ) { if ( update_post_meta( $this->id, '_stock_status', $status ) ) {
do_action( 'woocommerce_product_set_stock_status', $this->id, $status ); do_action( 'woocommerce_product_set_stock_status', $this->id, $status );
} }
@ -502,30 +506,12 @@ class WC_Product {
* @return bool * @return bool
*/ */
public function is_in_stock() { public function is_in_stock() {
if ( $this->managing_stock() ) { if ( $this->managing_stock() && $this->backorders_allowed() ) {
if ( $this->backorders_allowed() ) {
return true; return true;
} else { } elseif ( $this->managing_stock() && $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
if ( $this->get_total_stock() <= get_option( 'woocommerce_notify_no_stock_amount' ) ) {
return false; return false;
} else { } else {
if ( $this->stock_status === 'instock' ) { return $this->stock_status === 'instock';
return true;
} else {
return false;
}
}
}
} else {
if ( $this->stock_status === 'instock' ) {
return true;
} else {
return false;
}
} }
} }