Also consider refunded item quantity while increasing stock.

When updating an order, we call `wc_maybe_adjust_line_item_product_stock` to see if any item stock needs adjusting. We were not considering if an item has been refunded, which was causing incorrect stock adjustments.
This fix also takes in to account that an order has been refunded partially or fully.

Fixes #24489
This commit is contained in:
vedanshujain 2019-10-02 13:42:19 +05:30
parent 52cd7370b3
commit d2d0bf8466
1 changed files with 9 additions and 3 deletions

View File

@ -214,11 +214,13 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
return false;
}
$diff = $item_quantity - $already_reduced_stock;
$order = $item->get_order();
$refunded_item_quantity = $order->get_qty_refunded_for_item( $item->get_id() );
$diff = $item_quantity + $refunded_item_quantity - $already_reduced_stock;
if ( $diff < 0 ) {
$new_stock = wc_update_product_stock( $product, $diff * -1, 'increase' );
} else {
} elseif ( $diff > 0 ) {
$new_stock = wc_update_product_stock( $product, $diff, 'decrease' );
}
@ -226,9 +228,13 @@ function wc_maybe_adjust_line_item_product_stock( $item, $item_quantity = -1 ) {
return $new_stock;
}
$item->update_meta_data( '_reduced_stock', $item_quantity );
$item->update_meta_data( '_reduced_stock', $item_quantity + $refunded_item_quantity );
$item->save();
if ( 0 === $diff ) {
return false;
}
return array(
'from' => $new_stock + $diff,
'to' => $new_stock,