From ad94888ce3c78c16ef20404e6c5854b500ac0d00 Mon Sep 17 00:00:00 2001 From: Corey McKrill <916023+coreymckrill@users.noreply.github.com> Date: Thu, 29 Aug 2024 03:11:38 -0700 Subject: [PATCH] Ensure stock change notifications only fire when necessary (#51001) * Ensure stock change notifs only fire when necessary In #49583 we changed when the actions that trigger stock change notifications fire, to decouple them from order creation. However, it turns out what during product creation, the stock quantity update routines still run even when no stock is being set and even when the "manage stock" setting is set to false. Rather than try to untangle the web of why this happens (and potentially break other things in the process), this simply adds a check for the "manage stock" setting prior to firing the notif actions, and bail early if the product does not have managed stock. It also adds a unit test, which is really more of an integration test, to ensure this behavior. Fixes #50958 * Remove irrelevant doc block info In #49583 we added a second parameter to the `woocommerce_variation_set_stock` and `woocommerce_product_set_stock` action hooks, but it was later removed. However, we didn't remove the corresponding doc block info. * Add changelog file --------- Co-authored-by: Naman Malhotra --- .../fix-50958-non-tracked-stock-emails | 5 ++++ .../class-wc-product-data-store-cpt.php | 2 -- .../includes/wc-stock-functions.php | 4 ++++ .../php/includes/wc-stock-functions-tests.php | 24 +++++++++++++++++++ 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-50958-non-tracked-stock-emails diff --git a/plugins/woocommerce/changelog/fix-50958-non-tracked-stock-emails b/plugins/woocommerce/changelog/fix-50958-non-tracked-stock-emails new file mode 100644 index 00000000000..1df85f4876b --- /dev/null +++ b/plugins/woocommerce/changelog/fix-50958-non-tracked-stock-emails @@ -0,0 +1,5 @@ +Significance: patch +Type: fix +Comment: This is a further fix for #49583, which has not yet been released + + diff --git a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php index d4a43593e29..b5a611d6f8a 100644 --- a/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php +++ b/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php @@ -750,7 +750,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * Action to signal that the value of 'stock_quantity' for a variation has changed. * * @since 3.0 - * @since 9.2 Added $stock parameter. * * @param WC_Product $product The variation whose stock has changed. */ @@ -760,7 +759,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da * Action to signal that the value of 'stock_quantity' for a product has changed. * * @since 3.0 - * @since 9.2 Added $stock parameter. * * @param WC_Product $product The variation whose stock has changed. */ diff --git a/plugins/woocommerce/includes/wc-stock-functions.php b/plugins/woocommerce/includes/wc-stock-functions.php index bc49bc61dd9..1489a0e6630 100644 --- a/plugins/woocommerce/includes/wc-stock-functions.php +++ b/plugins/woocommerce/includes/wc-stock-functions.php @@ -284,6 +284,10 @@ function wc_trigger_stock_change_notifications( $order, $changes ) { * @return void */ function wc_trigger_stock_change_actions( $product ) { + if ( true !== $product->get_manage_stock() ) { + return; + } + $no_stock_amount = absint( get_option( 'woocommerce_notify_no_stock_amount', 0 ) ); $low_stock_amount = absint( wc_get_low_stock_amount( $product ) ); $stock_quantity = $product->get_stock_quantity(); diff --git a/plugins/woocommerce/tests/php/includes/wc-stock-functions-tests.php b/plugins/woocommerce/tests/php/includes/wc-stock-functions-tests.php index 72ec9a59875..90f312be98b 100644 --- a/plugins/woocommerce/tests/php/includes/wc-stock-functions-tests.php +++ b/plugins/woocommerce/tests/php/includes/wc-stock-functions-tests.php @@ -432,4 +432,28 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case { remove_action( 'woocommerce_no_stock', $callback ); } + + /** + * @testdox The wc_trigger_stock_change_actions function should only trigger actions if the product is set + * to manage stock. + */ + public function test_wc_trigger_stock_change_actions_bails_early_for_unmanaged_stock() { + $action_fired = false; + $callback = function () use ( &$action_fired ) { + $action_fired = true; + }; + add_action( 'woocommerce_no_stock', $callback ); + + $product = WC_Helper_Product::create_simple_product(); + + $this->assertFalse( $action_fired ); + + $product->set_manage_stock( true ); + $product->set_stock_quantity( 0 ); + $product->save(); + + $this->assertTrue( $action_fired ); + + remove_action( 'woocommerce_no_stock', $callback ); + } }