How to Programmatically Create WooCommerce Orders

While WooCommerce handles front-end checkouts seamlessly, advanced e-commerce applications often require programmatic order creation. Whether you are importing past orders from a legacy platform, syncing offline sales from a POS system, or generating orders automatically via a custom API webhook, you need to know how to build and save WooCommerce orders directly in PHP.

Instead of inserting raw data into custom database tables, WooCommerce provides the robust wc_create_order() function and order data objects that handle line items, tax calculations, and customer metadata securely.

In this tutorial, we will walk through how to create a complete WooCommerce order programmatically from scratch.

1. Creating the Order Object and Setting Customer Data

To initiate a new order, we use the core WooCommerce helper function wc_create_order(). You can pass the customer ID so that the order links directly to their user account.

Add the following helper function to your custom plugin or theme’s functions.php:

				
					if (!defined('ABSPATH')) {
    exit;
}

function wg_woo_create_programmatic_order($user_id, $product_id, $quantity = 1) {
    if (!function_exists('wc_create_order')) {
        return false;
    }

    // 1. Create the base order object linked to a user
    $order = wc_create_order([
        'customer_id' => $user_id,
        'customer_note' => __('Created programmatically via WebGarage integration.', 'webgarage'),
    ]);

    if (is_wp_error($order)) {
        return false;
    }

    // 2. Add products as line items
    $product = wc_get_product($product_id);
    if ($product) {
        $order->add_product($product, $quantity);
    }

    // 3. Populate billing and shipping addresses from user profile
    $customer = new WC_Customer($user_id);
    if ($customer->get_id()) {
        $address = [
            'first_name' => $customer->get_first_name(),
            'last_name'  => $customer->get_last_name(),
            'email'      => $customer->get_email(),
            'phone'      => $customer->get_billing_phone(),
            'address_1'  => $customer->get_billing_address_1(),
            'address_2'  => $customer->get_billing_address_2(),
            'city'       => $customer->get_billing_city(),
            'state'      => $customer->get_billing_state(),
            'postcode'   => $customer->get_billing_postcode(),
            'country'    => $customer->get_billing_country(),
        ];
        
        $order->set_address($address, 'billing');
        $order->set_address($address, 'shipping');
    }

    // 4. Set payment method details
    $order->set_payment_method('bacs'); // e.g., Direct Bank Transfer
    $order->set_payment_method_title(__('Direct Bank Transfer', 'webgarage'));

    // 5. Calculate totals, taxes, and save the order
    $order->calculate_totals();
    $order->update_status('processing', __('Order successfully generated programmatically.', 'webgarage'));

    return $order->get_id();
}
				
			

2. Adding Custom Meta Data to Programmatic Orders

If you need to attach extra transaction IDs, external reference numbers, or custom flags to your newly created order, you can use the $order->update_meta_data() method before saving:

				
					// Attach a custom external transaction reference
$order->update_meta_data('_wg_external_order_id', 'EXT-998877');
$order->save(); // Save metadata changes
				
			

3. Triggering Order Confirmation Emails

When orders are created programmatically via backend scripts, WooCommerce does not send customer notification emails by default. If you want to trigger the standard processing email, you can invoke the email action explicitly:

				
					// Trigger customer processing email notification
do_action('woocommerce_order_status_processing', $order->get_id(), $order);
				
			

Using native WooCommerce functions like wc_create_order() and $order->add_product() allows you to build powerful automated order pipelines, seamless migrations, and custom API integrations without breaking core store logic.

Be sure to check out our previous tutorials on WordPress development:
How to Programmatically Create MemberPress Subscriptions
How to Add & Save Custom WooCommerce Checkout Fields
How to Add Custom Tabs to the MemberPress Account

Need help with your project? We will be happy to help with your project. Feel free to contact us. Just reach out and we will reply soon.

Comments

No comments yet

Share Your Thoughts

Join the conversation!

Scroll to Top

Share Your Thoughts

Thank you for your comment!

Your email address will not be published. Required fields are marked *

Pro

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Standard

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project

Basic

Thank you for reaching out! We’ll be in touch within 24 hours to discuss your project