WC_REST_Orders_V1_Controller::create_order() protected WC 1.0
Create order.
{} It's a method of the class: WC_REST_Orders_V1_Controller{}
No Hooks.
Return
Int|WP_Error.
Usage
// protected - for code of main (parent) or child class $result = $this->create_order( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
Code of WC_REST_Orders_V1_Controller::create_order() WC REST Orders V1 Controller::create order WC 5.0.0
protected function create_order( $request ) {
try {
// Make sure customer exists.
if ( ! is_null( $request['customer_id'] ) && 0 !== $request['customer_id'] && false === get_user_by( 'id', $request['customer_id'] ) ) {
throw new WC_REST_Exception( 'woocommerce_rest_invalid_customer_id',__( 'Customer ID is invalid.', 'woocommerce' ), 400 );
}
// Make sure customer is part of blog.
if ( is_multisite() && ! is_user_member_of_blog( $request['customer_id'] ) ) {
add_user_to_blog( get_current_blog_id(), $request['customer_id'], 'customer' );
}
$order = $this->prepare_item_for_database( $request );
$order->set_created_via( 'rest-api' );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$order->calculate_totals();
$order->save();
// Handle set paid.
if ( true === $request['set_paid'] ) {
$order->payment_complete( $request['transaction_id'] );
}
return $order->get_id();
} catch ( WC_Data_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), $e->getErrorData() );
} catch ( WC_REST_Exception $e ) {
return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
}
}