Automattic\WooCommerce\Internal

RestockRefundedItemsAdjuster::initialize_restock_refunded_items()publicWC 1.0

Initializes the restock refunded items meta for order version less than 5.5.

Method of the class: RestockRefundedItemsAdjuster{}

No Hooks.

Return

null. Nothing (null).

Usage

$RestockRefundedItemsAdjuster = new RestockRefundedItemsAdjuster();
$RestockRefundedItemsAdjuster->initialize_restock_refunded_items( $order_id, $items );
$order_id(int) (required)
Order ID.
$items(array) (required)
Order items to save.

Notes

RestockRefundedItemsAdjuster::initialize_restock_refunded_items() code WC 9.3.3

public function initialize_restock_refunded_items( $order_id, $items ) {
	$order         = wc_get_order( $order_id );
	$order_version = $order->get_version();

	if ( version_compare( $order_version, '5.5', '>=' ) ) {
		return;
	}

	// If there are no refund lines, then this migration isn't necessary because restock related meta's wouldn't be set.
	if ( 0 === count( $order->get_refunds() ) ) {
		return;
	}

	if ( isset( $items['order_item_id'] ) ) {
		foreach ( $items['order_item_id'] as $item_id ) {
			$item = $this->order_factory::get_order_item( absint( $item_id ) );

			if ( ! $item ) {
				continue;
			}

			if ( 'line_item' !== $item->get_type() ) {
				continue;
			}

			// There could be code paths in custom code which don't update version number but still update the items.
			if ( '' !== $item->get_meta( '_restock_refunded_items', true ) ) {
				continue;
			}

			$refunded_item_quantity = abs( $order->get_qty_refunded_for_item( $item->get_id() ) );
			$item->add_meta_data( '_restock_refunded_items', $refunded_item_quantity, false );
			$item->save();
		}
	}
}