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 ); + } }