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 <naman03malhotra@gmail.com>
This commit is contained in:
Corey McKrill 2024-08-29 03:11:38 -07:00 committed by GitHub
parent 0a1af26785
commit ad94888ce3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 33 additions and 2 deletions

View File

@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: This is a further fix for #49583, which has not yet been released

View File

@ -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.
*/

View File

@ -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();

View File

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