WC_REST_Orders_V1_Controller::set_item() protected WC 1.0
Wrapper method to create/update order items. When updating, the item ID provided is checked to ensure it is associated with the order.
{} It's a method of the class: WC_REST_Orders_V1_Controller{}
Hooks from the method
Return
Null. Nothing.
Usage
// protected - for code of main (parent) or child class $result = $this->set_item( $order, $item_type, $posted );
- $order(WC_Order) (required)
- order
- $item_type(string) (required)
- -
- $posted(array) (required)
- item provided in the request body
Code of WC_REST_Orders_V1_Controller::set_item() WC REST Orders V1 Controller::set item WC 5.0.0
protected function set_item( $order, $item_type, $posted ) {
global $wpdb;
if ( ! empty( $posted['id'] ) ) {
$action = 'update';
} else {
$action = 'create';
}
$method = 'prepare_' . $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( $posted['id'] ),
absint( $order->get_id() )
) );
if ( is_null( $result ) ) {
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 );
/**
* Action hook to adjust item before save.
* @since 3.0.0
*/
do_action( 'woocommerce_rest_set_order_item', $item, $posted );
// Save or add to order
if ( 'create' === $action ) {
$order->add_item( $item );
} else {
$item->save();
}
}