Restock refunded products not working properly
-
Hi,
I believe I found an error in the Woocommerce codebase. As I didn’t find any other place to report this issue, I thought I’ll post it here.
The problem occurs when products are restocked during refunds and the admin order page is saved anytime after. This is because the order item meta data
_reduced_stock
and_restock_refunded_items
are deleted when the full amount of order_items is refunded inwc_restock_refunded_items()
, which then causeswc_maybe_adjust_line_item_product_stock()
to deduct the order items again from stock and thereby reversing the stock increase from the refund.The passage which causes this error, is Line 715-727 in wc-order-functions.php.
// Update _reduced_stock meta to track changes. $item_stock_reduced = $item_stock_reduced - $qty_to_refund; if ( 0 < $item_stock_reduced ) { // Keeps track of total running tally of reduced stock. $item->update_meta_data( '_reduced_stock', $item_stock_reduced ); // Keeps track of only refunded items that needs restock. $item->update_meta_data( '_restock_refunded_items', $qty_to_refund + $restock_refunded_items ); } else { $item->delete_meta_data( '_reduced_stock' ); $item->delete_meta_data( '_restock_refunded_items' ); }
In my opinion, the
0 <
comparison should at least be0 <=
, as a full refund results in$item_stock_reduced == $qty_to_refund
and further down the line to$item_stock_reduced = 0
. This is not greater zero, hence the item meta data will be deleted. Although, I generally do not understand, why this comparison is necessary in any case and the meta data values should be deleted in any scenario. One could just cap$qty_to_refund
to be maximally$item_stock_reduced
.Any thoughts? Am I correct in posting this here, or should I move this discussion elsewhere?
Thanks and best regards.
- The topic ‘Restock refunded products not working properly’ is closed to new replies.