Automatically transition stock statuses

This commit is contained in:
claudiulodro 2017-11-15 10:48:39 -08:00
parent db117d6344
commit 68e23911ba
2 changed files with 59 additions and 2 deletions

View File

@ -1307,6 +1307,10 @@ class WC_Product extends WC_Abstract_Legacy_Product {
} elseif ( $this->get_stock_quantity() <= get_option( 'woocommerce_notify_no_stock_amount' ) && 'no' === $this->get_backorders() ) {
$this->set_stock_status( 'outofstock' );
// If we are stock managing, backorders are allowed, and we don't have stock, force on backorder status.
} elseif ( $this->get_stock_quantity() <= 0 && 'no' !== $this->get_backorders() ) {
$this->set_stock_status( 'onbackorder' );
// If the stock level is changing and we do now have enough, force in stock status.
} elseif ( $this->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount' ) && array_key_exists( 'stock_quantity', $this->get_changes() ) ) {
$this->set_stock_status( 'instock' );
@ -1564,7 +1568,7 @@ class WC_Product extends WC_Abstract_Legacy_Product {
* @return bool
*/
public function is_on_backorder( $qty_in_cart = 0 ) {
return $this->managing_stock() && $this->backorders_allowed() && ( $this->get_stock_quantity() - $qty_in_cart ) < 0 ? true : false;
return 'onbackorder' === $this->get_stock_status();
}
/**

View File

@ -86,7 +86,6 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case {
*/
public function test_product_backorder_stock_status() {
$product = new WC_Product();
$product->set_stock_status( 'onbackorder' );
$this->assertEquals( 'onbackorder', $product->get_stock_status() );
@ -95,6 +94,60 @@ class WC_Tests_Product_Data extends WC_Unit_Test_Case {
$this->assertEquals( 'onbackorder', $product->get_stock_status() );
}
/**
* Test the automatic stock status changing when products are managing stock.
*
* @since 3.3.0
*/
public function test_product_auto_stock_status() {
$product = new WC_Product();
// Product should not have quantity and stock status should not be updated automatically if not managing stock.
$product->set_manage_stock( false );
$product->set_stock_quantity( 5 );
$product->set_stock_status( 'instock' );
$product->save();
$this->assertEquals( '', $product->get_stock_quantity() );
$this->assertEquals( 'instock', $product->get_stock_status() );
$product->set_stock_status( 'outofstock' );
$product->save();
$this->assertEquals( 'outofstock', $product->get_stock_status() );
$product->set_manage_stock( true );
// Product should be out of stock if managing orders, no backorders allowed, and quantity too low.
$product->set_stock_quantity( 0 );
$product->set_stock_status( 'instock' );
$product->set_backorders( 'no' );
$product->save();
$this->assertEquals( 0, $product->get_stock_quantity() );
$this->assertEquals( 'outofstock', $product->get_stock_status() );
// Product should be on backorder if managing orders, backorders allowed, and quantity too low.
$product->set_stock_quantity( 0 );
$product->set_stock_status( 'instock' );
$product->set_backorders( 'yes' );
$product->save();
$this->assertEquals( 0, $product->get_stock_quantity() );
$this->assertEquals( 'onbackorder', $product->get_stock_status() );
// Product should go to in stock if backordered and inventory increases.
$product->set_stock_quantity( 5 );
$product->set_stock_status( 'onbackorder' );
$product->set_backorders( 'notify' );
$product->save();
$this->assertEquals( 5, $product->get_stock_quantity() );
$this->assertEquals( 'instock', $product->get_stock_status() );
// Product should go to in stock if out of stock and inventory increases.
$product->set_stock_quantity( 3 );
$product->set_stock_status( 'outofstock' );
$product->set_backorders( 'no' );
$product->save();
$this->assertEquals( 3, $product->get_stock_quantity() );
$this->assertEquals( 'instock', $product->get_stock_status() );
}
/**
* Test product term setters and getters
* @since 3.0.0