WC_API_Orders::set_item( WC_Order $order, string $item_type, array $item, string $action )

Wrapper method to create/update order items


Description Description

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


Parameters Parameters

$order

(Required) order

$item_type

(Required)

$item

(Required) item provided in the request body

$action

(Required) either 'create' or 'update'


Top ↑

Source Source

File: includes/legacy/api/v2/class-wc-api-orders.php

	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 );
	}

Top ↑

Changelog Changelog

Changelog
Version Description
2.2 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.