Automattic\WooCommerce\Internal\Fulfillments

FulfillmentsManager::update_fulfillments_after_refundpublicWC 1.0

Update fulfillments after a refund is created.

Method of the class: FulfillmentsManager{}

No Hooks.

Returns

null. Nothing (null).

Usage

$FulfillmentsManager = new FulfillmentsManager();
$FulfillmentsManager->update_fulfillments_after_refund( $refund_id ): void;
$refund_id(int) (required)
The ID of the refund created.

FulfillmentsManager::update_fulfillments_after_refund() code WC 10.3.3

public function update_fulfillments_after_refund( int $refund_id ): void {
	// Get the order object.
	$refund = $refund_id ? wc_get_order( $refund_id ) : null;
	if ( ! $refund instanceof WC_Order_Refund ) {
		return; // If the order is not valid, do nothing.
	}

	$order_id = $refund->get_parent_id();
	if ( ! $order_id ) {
		return; // If the refund does not have a parent order, do nothing.
	}
	$order = wc_get_order( $order_id );
	if ( ! $order instanceof \WC_Order ) {
		return; // If the order is not valid, do nothing.
	}

	// If there are no refunded items, we can skip the fulfillment update.
	$items_refunded = FulfillmentUtils::get_refunded_items( $order );
	if ( empty( $items_refunded ) ) {
		return; // No items were refunded, so no need to update fulfillments.
	}

	// Get the fulfillments data store and read all fulfillments for the order.
	$fulfillments_data_store = wc_get_container()->get( FulfillmentsDataStore::class );
	$fulfillments            = $fulfillments_data_store->read_fulfillments( \WC_Order::class, (string) $order_id );
	if ( empty( $fulfillments ) ) {
		return; // No fulfillments found for the order.
	}

	// Get all refunded items from the order.
	$pending_items_without_refunds = FulfillmentUtils::get_pending_items( $order, $fulfillments, false );
	$pending_items_without_refunds = array_map(
		function ( $item ) {
			return array(
				'item_id' => $item['item_id'],
				'qty'     => $item['qty'],
			);
		},
		$pending_items_without_refunds
	);

	// Check if the refunded items can be removed from pending items.
	foreach ( $items_refunded as $item_id => &$refunded_qty ) {
		$pending_item_record = array_filter(
			$pending_items_without_refunds,
			function ( $item ) use ( $item_id ) {
				return isset( $item['item_id'] ) && $item['item_id'] === $item_id;
			}
		);
		if ( ! empty( $pending_item_record ) ) {
			$pending_item_record = reset( $pending_item_record );
			if ( isset( $pending_item_record['qty'] ) && $pending_item_record['qty'] > 0 ) {
				// If the pending item quantity is greater than the refunded quantity, reduce it.
				$refunded_qty -= $pending_item_record['qty'];
			}
		}
	}

	// If all refunded items can be removed from pending items, we can skip the fulfillment update.
	$items_need_removal_from_fulfillments = array_filter(
		$items_refunded,
		function ( $actual_qty ) {
			return $actual_qty > 0;
		}
	);

	if ( empty( $items_need_removal_from_fulfillments ) ) {
		return;
	}

	// Now we need to adjust the fulfillments based on the refunded items.
	// Loop through each fulfillment and adjust the items based on the refunded quantities.
	// We will remove items from fulfillments if they are fully refunded, or reduce their quantity if partially refunded.
	// If a fulfillment has no items left after adjustment, we will delete it.
	// If a fulfillment has items left, we will update the fulfillment with the new items.
	foreach ( $fulfillments as $fulfillment ) {
		if ( ! $fulfillment instanceof Fulfillment ) {
			continue; // Skip if the fulfillment is not an instance of Fulfillment.
		}

		if ( $fulfillment->get_is_fulfilled() ) {
			continue; // Skip if the fulfillment is already fulfilled. We don't remove items from fulfilled fulfillments.
		}

		// Get the items from the fulfillment.
		$items = $fulfillment->get_items();
		if ( empty( $items ) ) {
			continue; // Skip if there are no items in the fulfillment.
		}

		// Adjust the items based on the refund.
		$new_items = array();
		foreach ( $items as $item ) {
			if ( isset( $item['qty'] ) && isset( $item['item_id'] ) && isset( $items_need_removal_from_fulfillments[ $item['item_id'] ] ) ) {
				if ( $items_need_removal_from_fulfillments[ $item['item_id'] ] <= $item['qty'] ) {
					// If the refunded quantity is less than or equal to the item quantity, reduce the item quantity.
					$item['qty'] -= $items_need_removal_from_fulfillments[ $item['item_id'] ];
					$items_need_removal_from_fulfillments[ $item['item_id'] ] = 0; // Set refunded quantity to zero after adjustment.
				} else {
					// If the refunded quantity is greater than the item quantity, set the item quantity to zero.
					$item['qty'] = 0;
					$items_need_removal_from_fulfillments[ $item['item_id'] ] -= $item['qty']; // Reduce the refunded quantity.
				}
				$new_items[] = $item; // Add the adjusted item to the new items array.
			} else {
				$new_items[] = $item; // If the item is not in the refunded items, keep it as is.
			}
		}

		$new_items = array_filter(
			$new_items,
			function ( $item ) {
				return isset( $item['qty'] ) && $item['qty'] > 0; // Only keep items with a positive quantity.
			}
		);

		if ( empty( $new_items ) ) {
			// If no items remain after adjustment, delete the fulfillment.
			$fulfillment->delete();
		} else {
			// Update the fulfillment items with the new items.
			$fulfillment->set_items( $new_items );
			$fulfillment->save();
		}
	}

	$this->update_fulfillment_status( $order, $fulfillments );
}