Limit the new refund restock logic to order version 5.4+

This commit is contained in:
roykho 2021-04-29 06:30:53 -07:00
parent 6432e06e7d
commit 87047c77df
No known key found for this signature in database
GPG Key ID: 7B36C0EA25795714
3 changed files with 35 additions and 5 deletions

View File

@ -216,13 +216,15 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
$already_reduced_stock = wc_stock_amount( $item->get_meta( '_reduced_stock', true ) );
$restock_refunded_items = wc_stock_amount( $item->get_meta( '_restock_refunded_items', true ) );
$order = $item->get_order();
$order_version = $order->get_version();
$refunded_item_quantity = $order->get_qty_refunded_for_item( $item->get_id() );
$diff = $item_quantity - $restock_refunded_items - $already_reduced_stock;
$_new_reduced_stock = $item_quantity - $restock_refunded_items;
if ( 0 === $restock_refunded_items ) {
$diff = $item_quantity + $refunded_item_quantity - $already_reduced_stock;
$_new_reduced_stock = $item_quantity - $refunded_item_quantity;
$diff = $item_quantity - $restock_refunded_items - $already_reduced_stock;
if ( version_compare( $order_version, '5.4', '<' ) ) {
$diff = $item_quantity + $refunded_item_quantity - $already_reduced_stock;
$_new_reduced_stock = $item_quantity + $refunded_item_quantity;
}
/*

View File

@ -24,7 +24,7 @@ final class WooCommerce {
*
* @var string
*/
public $version = '5.3.0';
public $version = '5.4.0';
/**
* WooCommerce Schema version.

View File

@ -289,6 +289,7 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
/**
* Test adjust line item function when order item is refunded with restock and then update order.
* This only works with orders placed after WC 5.4.
*
* @link https://github.com/woocommerce/woocommerce/issues/29502.
*/
@ -301,6 +302,11 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
$product->save();
$order = WC_Helper_Order::create_order();
if ( version_compare( $order->get_version(), '5.4', '<' ) ) {
return;
}
$order->set_status( 'on-hold' );
$order_item_id = $order->add_product( $product, 10 );
$order_item = new WC_Order_Item_Product( $order_item_id );
@ -335,10 +341,19 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
// Stocks should remain unchanged from after restocking via refund operation.
$this->assertEquals( 95, $product->get_stock_quantity() );
// Repeating steps above again to make sure nothing changes.
wc_maybe_adjust_line_item_product_stock( $order_item );
$product = wc_get_product( $product->get_id() );
// Stocks should remain unchanged from after restocking via refund operation.
$this->assertEquals( 95, $product->get_stock_quantity() );
}
/**
* Test adjust line item function when order item is refunded without restock and then update order.
* This only works with orders placed after WC 5.4.
*
* @link https://github.com/woocommerce/woocommerce/issues/29502.
*/
@ -351,6 +366,11 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
$product->save();
$order = WC_Helper_Order::create_order();
if ( version_compare( $order->get_version(), '5.4', '<' ) ) {
return;
}
$order->set_status( 'on-hold' );
$order_item_id = $order->add_product( $product, 10 );
$order_item = new WC_Order_Item_Product( $order_item_id );
@ -385,5 +405,13 @@ class WC_Admin_Functions_Test extends \WC_Unit_Test_Case {
// Stocks should remain unchanged from the original order.
$this->assertEquals( 90, $product->get_stock_quantity() );
// Repeating steps above again to make sure nothing changes.
wc_maybe_adjust_line_item_product_stock( $order_item );
$product = wc_get_product( $product->get_id() );
// Stocks should remain unchanged from the original order.
$this->assertEquals( 90, $product->get_stock_quantity() );
}
}