wc_create_order( array $args = array() )
Create a new order programmatically.
Description Description
Returns a new order object on success which can then be used to add additional data.
Parameters Parameters
- $args
-
(Optional) Order arguments.
Default value: array()
Return Return
(WC_Order|WP_Error)
Source Source
File: includes/wc-core-functions.php
function wc_create_order( $args = array() ) {
$default_args = array(
'status' => null,
'customer_id' => null,
'customer_note' => null,
'parent' => null,
'created_via' => null,
'cart_hash' => null,
'order_id' => 0,
);
try {
$args = wp_parse_args( $args, $default_args );
$order = new WC_Order( $args['order_id'] );
// Update props that were set (not null).
if ( ! is_null( $args['parent'] ) ) {
$order->set_parent_id( absint( $args['parent'] ) );
}
if ( ! is_null( $args['status'] ) ) {
$order->set_status( $args['status'] );
}
if ( ! is_null( $args['customer_note'] ) ) {
$order->set_customer_note( $args['customer_note'] );
}
if ( ! is_null( $args['customer_id'] ) ) {
$order->set_customer_id( is_numeric( $args['customer_id'] ) ? absint( $args['customer_id'] ) : 0 );
}
if ( ! is_null( $args['created_via'] ) ) {
$order->set_created_via( sanitize_text_field( $args['created_via'] ) );
}
if ( ! is_null( $args['cart_hash'] ) ) {
$order->set_cart_hash( sanitize_text_field( $args['cart_hash'] ) );
}
// Set these fields when creating a new order but not when updating an existing order.
if ( ! $args['order_id'] ) {
$order->set_currency( get_woocommerce_currency() );
$order->set_prices_include_tax( 'yes' === get_option( 'woocommerce_prices_include_tax' ) );
$order->set_customer_ip_address( WC_Geolocation::get_ip_address() );
$order->set_customer_user_agent( wc_get_user_agent() );
}
// Update other order props set automatically.
$order->save();
} catch ( Exception $e ) {
return new WP_Error( 'error', $e->getMessage() );
}
return $order;
}