WC_REST_Legacy_Orders_Controller::create_order()protectedWC 1.0

Deprecated from version 3.0.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Create order.

Method of the class: WC_REST_Legacy_Orders_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.

Changelog

Deprecated since 3.0.0

WC_REST_Legacy_Orders_Controller::create_order() code WC 8.6.1

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() ) );
	}
}