WC_REST_Orders_V2_Controller::set_item()
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_REST_Orders_V2_Controller{}
Hooks from the method
Return
null
. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->set_item( $order, $item_type, $posted );
- $order(WC_Order) (required)
- order object.
- $item_type(string) (required)
- The item type.
- $posted(array) (required)
- item provided in the request body.
WC_REST_Orders_V2_Controller::set_item() WC REST Orders V2 Controller::set item code WC 8.7.0
protected function set_item( $order, $item_type, $posted ) { global $wpdb; if ( ! empty( $posted['id'] ) ) { $action = 'update'; } else { $action = 'create'; } $method = 'prepare_' . $item_type; $item = null; // Verify provided line item ID is associated with order. if ( 'update' === $action ) { $item = $order->get_item( absint( $posted['id'] ), false ); if ( ! $item ) { throw new WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 ); } } // Prepare item data. $item = $this->$method( $posted, $action, $item ); /** * Allow extensions be notified before the item before is saved. * * @param WC_Order_Item $item The item object. * @param array $posted The item data. * * @since 4.5.0. */ do_action( 'woocommerce_rest_set_order_item', $item, $posted ); // If creating the order, add the item to it. if ( 'create' === $action ) { $order->add_item( $item ); } else { $item->save(); } }