From c69e1f5799d002f48e2cc82c5145874558fccc4c Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 24 Sep 2020 00:54:24 +0530 Subject: [PATCH 1/2] Remove protection as we already diff and update only if needed. In #26642 we removed adding reduced_stock meta when adding new order item to prevent ghost entries, but in inadvertently exposed an underlying bug where _reduced_stock meta was getting set to 0 if its emtpy. We were then checking the presence of this meta, but also not reducing the stock in case it was not set. --- includes/admin/wc-admin-functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/wc-admin-functions.php b/includes/admin/wc-admin-functions.php index bc9ae3c3ce9..26be8a6a88d 100644 --- a/includes/admin/wc-admin-functions.php +++ b/includes/admin/wc-admin-functions.php @@ -210,7 +210,7 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) { $item_quantity = wc_stock_amount( $item_quantity >= 0 ? $item_quantity : $item->get_quantity() ); $already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) ); - if ( ! $product || ! $product->managing_stock() || ! $already_reduced_stock || $item_quantity === $already_reduced_stock ) { + if ( ! $product || ! $product->managing_stock() ) { return false; } From d51dd268168145ba076ede438d8416e3e2ccf6be Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Fri, 25 Sep 2020 19:41:20 +0530 Subject: [PATCH 2/2] Add unit tests. --- ....php => class-wc-admin-functions-test.php} | 28 +++++++++++++++++++ tests/php/includes/class-wc-ajax-test.php | 1 - 2 files changed, 28 insertions(+), 1 deletion(-) rename tests/php/includes/admin/{wc-admin-functions-test.php => class-wc-admin-functions-test.php} (60%) diff --git a/tests/php/includes/admin/wc-admin-functions-test.php b/tests/php/includes/admin/class-wc-admin-functions-test.php similarity index 60% rename from tests/php/includes/admin/wc-admin-functions-test.php rename to tests/php/includes/admin/class-wc-admin-functions-test.php index 3910dac8b50..b881fe9a54d 100644 --- a/tests/php/includes/admin/wc-admin-functions-test.php +++ b/tests/php/includes/admin/class-wc-admin-functions-test.php @@ -41,4 +41,32 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case { // Restore REQUEST_URI. $_SERVER['REQUEST_URI'] = $default_uri; } + + /** + * Test adjust line item function when order does not have meta `_reduced_stock` already. + * + * @link https://github.com/woocommerce/woocommerce/issues/27445. + */ + public function test_wc_maybe_adjust_line_item_product_stock() { + $product = WC_Helper_Product::create_simple_product(); + $product->set_manage_stock( true ); + $product->set_stock_quantity( 1000 ); + $product->save(); + + $order = WC_Helper_Order::create_order(); + $order->set_status( 'processing' ); + $order_item_id = $order->add_product( $product, 10 ); + + // Stocks have not reduced yet. + $product = wc_get_product( $product->get_id() ); + $this->assertEquals( 1000, $product->get_stock_quantity() ); + + $order_item = new WC_Order_Item_Product( $order_item_id ); + wc_maybe_adjust_line_item_product_stock( $order_item ); + + // Stocks should have been reduced now. + $product = wc_get_product( $product->get_id() ); + $this->assertEquals( 990, $product->get_stock_quantity() ); + } + } diff --git a/tests/php/includes/class-wc-ajax-test.php b/tests/php/includes/class-wc-ajax-test.php index afce0cb159a..104b8069d3d 100644 --- a/tests/php/includes/class-wc-ajax-test.php +++ b/tests/php/includes/class-wc-ajax-test.php @@ -84,5 +84,4 @@ class WC_AJAX_Test extends \WC_Unit_Test_Case { } } } - }