How to Create WooCommerce Order Programmatically

Create WooCommerce Orders Programmatically

To create WooCommerce orders programmatically in WordPress, use the wc_create_order() function. This allows you to add products, fees, shipping, coupons, and more. Follow this guide for a step-by-step process.

 

Step 1: Set up WordPress and WooCommerce

Ensure that you have WordPress with the WooCommerce plugin installed and activated. You can download WooCommerce from the WordPress plugin repository or install it directly from your WordPress dashboard.

 

Step 2: Create a Custom Plugin or Edit functions.php

It’s best to add custom code to a plugin or your theme’s functions.php file for easy management. Create a new plugin or modify the functions.php file.

 

Step 3: Define the Order Creation Function

Start by defining a function to create the WooCommerce order programmatically. This function will handle the order process:

// In your custom plugin or functions.php
function create_woocommerce_order() {
    // Your order creation code goes here
}

Step 4: Add Order Items

Add products to the order. Use the wc_create_order() function to add multiple products:

// In create_woocommerce_order() function
$order = wc_create_order();
$products = array(
    array( 'product_id' => 1, 'quantity' => 2 ),
    array( 'product_id' => 2, 'quantity' => 1 ),
);
foreach ($products as $product) {
    $product_id = $product['product_id'];
    $quantity = $product['quantity'];
    // Add the order item for each product
}

Step 5: Add Fees

To add fees, use the WC_Order_Item_Fee class:

// In create_woocommerce_order() function
$fee = new WC_Order_Item_Fee();
$fee->set_props(array(
    'name'      => 'Additional Fee',
    'amount'    => 10.00,
    'tax_class' => '',
));
$fee->save();
$order->add_item($fee);

Step 6: Add Shipping

For shipping, use the WC_Order_Item_Shipping class:

// In create_woocommerce_order() function
$shipping_item = new WC_Order_Item_Shipping();
$shipping_item->set_method_title('Flat Rate');
$shipping_item->set_total(5.00); 
$shipping_item->save();
$order->add_item($shipping_item);

Step 7: Add Coupons

If you want to add coupons, use the apply_coupon() method:

// In create_woocommerce_order() function
$coupon_code = 'SUMMER2023';
$coupon = new WC_Coupon($coupon_code);
$order->apply_coupon($coupon);

Step 8: Add Billing and Shipping Information

Set customer billing and shipping details:

// In create_woocommerce_order() function
$customer_data = array(
    'first_name' => 'John',
    'last_name'  => 'Doe',
    'email'      => 'john.doe@example.com',
);
$order->set_customer_id(0); 
$order->set_address($customer_data, 'billing');
$order->set_address($customer_data, 'shipping');

Step 9: Add Payment Method

Set the payment method for the order:

// In create_woocommerce_order() function
$payment_method = 'bacs'; // Bank Transfer
$order->set_payment_method($payment_method);

Step 10: Assign Order Status

Set the order status using the set_status() method:

// In create_woocommerce_order() function
$order_status = 'processing';
$order->set_status($order_status);

Step 11: Save the Order

Save the order after completing all the steps:

// In create_woocommerce_order() function
$order->save();
return $order->get_id();

Step 12: Trigger Order Creation

Trigger the order creation process when needed:

// In your custom plugin or functions.php
function create_woocommerce_order_endpoint() {
    create_woocommerce_order();
    // Add code to redirect or display a success message
}
add_action('init', 'create_woocommerce_order_endpoint');

Conclusion

With these steps, you can easily create WooCommerce orders programmatically. Customize the function based on your requirements, and handle error checks for a smoother user experience.

 

External Links:

Scroll to Top

Request A Quote