WC_API_Orders::set_item()protectedWC 2.2

Wrapper method to create/update order items

When updating, the item ID provided is checked to ensure it is associated with the order.

Method of the class: WC_API_Orders{}

No Hooks.

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->set_item( $order, $item_type, $item, $action );
$order(\WC_Order) (required)
order
$item_type(string) (required)
-
$item(array) (required)
item provided in the request body
$action(string) (required)
either 'create' or 'update'

Changelog

Since 2.2 Introduced.

WC_API_Orders::set_item() code WC 8.6.1

protected function set_item( $order, $item_type, $item, $action ) {
	global $wpdb;

	$set_method = "set_{$item_type}";

	// verify provided line item ID is associated with order
	if ( 'update' === $action ) {

		$result = $wpdb->get_row(
			$wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d AND order_id = %d",
			absint( $item['id'] ),
			absint( $order->get_id() )
		) );

		if ( is_null( $result ) ) {
			throw new WC_API_Exception( 'woocommerce_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 );
		}
	}

	$this->$set_method( $order, $item, $action );
}