Automattic\WooCommerce\Internal\RestApi\Routes\V4\Orders

UpdateUtils::update_line_itemsprotectedWC 1.0

Update line items from an array of line item data for an order. Non-posted line items are removed.

Method of the class: UpdateUtils{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->update_line_items( $order, $line_items, $line_items_type );
$order(WC_Order) (required)
The order to update the line items for.
$line_items(array) (required)
The line items to update.
$line_items_type(string)
The type of line items to update.
Default: 'line_item'

UpdateUtils::update_line_items() code WC 10.4.3

protected function update_line_items( WC_Order $order, array $line_items, string $line_items_type = 'line_item' ) {
	if ( ! in_array( $line_items_type, array( 'line_item', 'shipping', 'fee', 'coupon' ), true ) ) {
		throw new WC_REST_Exception( 'woocommerce_rest_invalid_line_items_type', esc_html__( 'Invalid line items type.', 'woocommerce' ), 400 );
	}

	// Get existing items from the order. Any items that are not in the $line_items array will be removed.
	$existing_items     = $order->get_items( $line_items_type );
	$processed_item_ids = array();
	foreach ( $line_items as $line_item_data ) {
		if ( ! is_array( $line_item_data ) ) {
			continue;
		}
		if ( $this->item_is_null_or_zero( $line_item_data ) ) {
			if ( $line_item_data['id'] ) {
				$this->remove_item_from_order( $order, $line_items_type, (int) $line_item_data['id'] );
			}
			continue;
		}
		$processed_item_ids[] = $this->update_line_item( $order, $line_items_type, $line_item_data );
	}

	// Remove any pre-existing items that were not posted.
	foreach ( $existing_items as $existing_item ) {
		if ( ! in_array( $existing_item->get_id(), $processed_item_ids, true ) ) {
			$this->remove_item_from_order( $order, $line_items_type, $existing_item->get_id() );
		}
	}
}