WC_REST_Orders_V1_Controller::prepare_item_for_database()
Prepare a single order for create.
Method of the class: WC_REST_Orders_V1_Controller{}
Hooks from the method
Return
WP_Error|WC_Order
. $data Object.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_item_for_database( $request );
- $request(WP_REST_Request) (required)
- Request object.
WC_REST_Orders_V1_Controller::prepare_item_for_database() WC REST Orders V1 Controller::prepare item for database code WC 9.4.2
protected function prepare_item_for_database( $request ) { $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 'billing' : case 'shipping' : $this->update_address( $order, $value, $key ); break; case 'line_items' : case 'shipping_lines' : case 'fee_lines' : case 'coupon_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'] ) ) { $order->remove_item( $item['id'] ); } else { $this->set_item( $order, $key, $item ); } } } } break; default : if ( is_callable( array( $order, "set_{$key}" ) ) ) { $order->{"set_{$key}"}( $value ); } break; } } } /** * Filter the data for the insert. * * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being * prepared for the response. * * @param WC_Order $order The order object. * @param WP_REST_Request $request Request object. */ return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}", $order, $request ); }