WC_REST_Orders_Controller::prepare_object_for_database()
Prepare a single order for create or update.
Method of the class: WC_REST_Orders_Controller{}
Hooks from the method
Return
WP_Error|WC_Data
.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_object_for_database( $request, $creating );
- $request(WP_REST_Request) (required)
- Request object.
- $creating(true|false)
- If is creating a new object.
Default: false
WC_REST_Orders_Controller::prepare_object_for_database() WC REST Orders Controller::prepare object for database code WC 9.4.2
protected function prepare_object_for_database( $request, $creating = false ) { $id = isset( $request['id'] ) ? absint( $request['id'] ) : 0; $order = new WC_Order( $id ); $schema = $this->get_item_schema(); $data_keys = array_keys( array_filter( $schema['properties'], array( $this, 'filter_writable_props' ) ) ); // Handle all writable props. foreach ( $data_keys as $key ) { $value = $request[ $key ]; if ( ! is_null( $value ) ) { switch ( $key ) { case 'coupon_lines': case 'status': // Change should be done later so transitions have new data. break; case 'billing': case 'shipping': $this->update_address( $order, $value, $key ); break; case 'line_items': case 'shipping_lines': case 'fee_lines': if ( is_array( $value ) ) { foreach ( $value as $item ) { if ( is_array( $item ) ) { if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) { $this->remove_item( $order, $key, $item['id'] ); } else { $this->set_item( $order, $key, $item ); } } } } break; case 'meta_data': if ( is_array( $value ) ) { foreach ( $value as $meta ) { $order->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); } } break; default: if ( is_callable( array( $order, "set_{$key}" ) ) ) { $order->{"set_{$key}"}( $value ); } break; } } } /** * Filters an object before it is inserted via the REST API. * * The dynamic portion of the hook name, `$this->post_type`, * refers to the object type slug. * * @since 7.4.0 * * @param WC_Data $order Object object. * @param WP_REST_Request $request Request object. * @param bool $creating If is creating a new object. */ return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $order, $request, $creating ); }