Revert low stock notification changes (#51441)

* Revert - Change when stock notif emails are triggered

* added changelog

* restored comment

* restored more comment

* lint fixes

* Add docblock.

---------

Co-authored-by: Vedanshu Jain <vedanshu.jain.2012@gmail.com>
This commit is contained in:
Naman Malhotra 2024-09-18 14:12:23 +03:00 committed by GitHub
parent 6a83e8d301
commit 3df48f2bd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 121 deletions

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Revert - changes related to low stock product notification

View File

@ -656,21 +656,21 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// Fire actions to let 3rd parties know the stock is about to be changed. // Fire actions to let 3rd parties know the stock is about to be changed.
if ( $product->is_type( 'variation' ) ) { if ( $product->is_type( 'variation' ) ) {
/** /**
* Action to signal that the value of 'stock_quantity' for a variation is about to change. * Action to signal that the value of 'stock_quantity' for a variation is about to change.
* *
* @param WC_Product $product The variation whose stock is about to change. * @since 4.9
* *
* @since 4.9 * @param int $product The variation whose stock is about to change.
*/ */
do_action( 'woocommerce_variation_before_set_stock', $product ); do_action( 'woocommerce_variation_before_set_stock', $product );
} else { } else {
/** /**
* Action to signal that the value of 'stock_quantity' for a product is about to change. * Action to signal that the value of 'stock_quantity' for a product is about to change.
* *
* @param WC_Product $product The product whose stock is about to change. * @since 4.9
* *
* @since 4.9 * @param int $product The product whose stock is about to change.
*/ */
do_action( 'woocommerce_product_before_set_stock', $product ); do_action( 'woocommerce_product_before_set_stock', $product );
} }
break; break;

View File

@ -242,10 +242,31 @@ function wc_trigger_stock_change_notifications( $order, $changes ) {
return; return;
} }
$order_notes = array(); $order_notes = array();
$no_stock_amount = absint( get_option( 'woocommerce_notify_no_stock_amount', 0 ) );
foreach ( $changes as $change ) { foreach ( $changes as $change ) {
$order_notes[] = $change['product']->get_formatted_name() . ' ' . $change['from'] . '&rarr;' . $change['to']; $order_notes[] = $change['product']->get_formatted_name() . ' ' . $change['from'] . '&rarr;' . $change['to'];
$low_stock_amount = absint( wc_get_low_stock_amount( wc_get_product( $change['product']->get_id() ) ) );
if ( $change['to'] <= $no_stock_amount ) {
/**
* Action to signal that the value of 'stock_quantity' for a variation is about to change.
*
* @since 4.9
*
* @param int $product The variation whose stock is about to change.
*/
do_action( 'woocommerce_no_stock', wc_get_product( $change['product']->get_id() ) );
} elseif ( $change['to'] <= $low_stock_amount ) {
/**
* Action to signal that the value of 'stock_quantity' for a product is about to change.
*
* @since 4.9
*
* @param int $product The product whose stock is about to change.
*/
do_action( 'woocommerce_low_stock', wc_get_product( $change['product']->get_id() ) );
}
if ( $change['to'] < 0 ) { if ( $change['to'] < 0 ) {
/** /**
@ -312,8 +333,6 @@ function wc_trigger_stock_change_actions( $product ) {
do_action( 'woocommerce_low_stock', $product ); do_action( 'woocommerce_low_stock', $product );
} }
} }
add_action( 'woocommerce_variation_set_stock', 'wc_trigger_stock_change_actions' );
add_action( 'woocommerce_product_set_stock', 'wc_trigger_stock_change_actions' );
/** /**
* Increase stock levels for items within an order. * Increase stock levels for items within an order.
@ -485,11 +504,8 @@ function wc_get_low_stock_amount( WC_Product $product ) {
$low_stock_amount = $product->get_low_stock_amount(); $low_stock_amount = $product->get_low_stock_amount();
if ( '' === $low_stock_amount && $product->is_type( 'variation' ) ) { if ( '' === $low_stock_amount && $product->is_type( 'variation' ) ) {
$parent_product = wc_get_product( $product->get_parent_id() ); $product = wc_get_product( $product->get_parent_id() );
$low_stock_amount = $product->get_low_stock_amount();
if ( $parent_product instanceof WC_Product ) {
$low_stock_amount = $parent_product->get_low_stock_amount();
}
} }
if ( '' === $low_stock_amount ) { if ( '' === $low_stock_amount ) {

View File

@ -356,104 +356,4 @@ class WC_Stock_Functions_Tests extends \WC_Unit_Test_Case {
$this->assertIsIntAndEquals( $site_wide_low_stock_amount, wc_get_low_stock_amount( $var1 ) ); $this->assertIsIntAndEquals( $site_wide_low_stock_amount, wc_get_low_stock_amount( $var1 ) );
} }
/**
* @testdox Test that the `woocommerce_low_stock` action fires when a product stock hits the low stock threshold.
*/
public function test_wc_update_product_stock_low_stock_action() {
$product = WC_Helper_Product::create_simple_product();
$product->set_manage_stock( true );
$product->save();
$low_stock_amount = wc_get_low_stock_amount( $product );
$initial_stock = $low_stock_amount + 2;
wc_update_product_stock( $product->get_id(), $initial_stock );
$action_fired = false;
$callback = function () use ( &$action_fired ) {
$action_fired = true;
};
add_action( 'woocommerce_low_stock', $callback );
// Test with `wc_update_product_stock`.
wc_update_product_stock( $product->get_id(), 1, 'decrease' );
$this->assertFalse( $action_fired );
wc_update_product_stock( $product->get_id(), 1, 'decrease' );
$this->assertTrue( $action_fired );
$action_fired = false;
// Test with the data store.
$product->set_stock_quantity( $initial_stock );
$product->save();
$this->assertFalse( $action_fired );
$product->set_stock_quantity( $low_stock_amount );
$product->save();
$this->assertTrue( $action_fired );
remove_action( 'woocommerce_low_stock', $callback );
}
/**
* @testdox Test that the `woocommerce_no_stock` action fires when a product stock hits the no stock threshold.
*/
public function test_wc_update_product_stock_no_stock_action() {
$product = WC_Helper_Product::create_simple_product();
$product->set_manage_stock( true );
$product->save();
$no_stock_amount = get_option( 'woocommerce_notify_no_stock_amount', 0 );
$initial_stock = $no_stock_amount + 2;
wc_update_product_stock( $product->get_id(), $initial_stock );
$action_fired = false;
$callback = function () use ( &$action_fired ) {
$action_fired = true;
};
add_action( 'woocommerce_no_stock', $callback );
// Test with `wc_update_product_stock`.
wc_update_product_stock( $product->get_id(), 1, 'decrease' );
$this->assertFalse( $action_fired );
wc_update_product_stock( $product->get_id(), 1, 'decrease' );
$this->assertTrue( $action_fired );
$action_fired = false;
// Test with the data store.
$product->set_stock_quantity( $initial_stock );
$product->save();
$this->assertFalse( $action_fired );
$product->set_stock_quantity( $no_stock_amount );
$product->save();
$this->assertTrue( $action_fired );
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 );
}
} }