You can automate the creation of WooCommerce orders in WordPress by using the wc_create_order()
function. This guide will walk you through the necessary steps to programmatically add products, fees, shipping, and more.
Step 1: Install WooCommerce and WordPress
Ensure you have WooCommerce installed and activated on your WordPress website. WooCommerce is available in the WordPress plugin directory.
Step 2: Create a Plugin or Modify functions.php
It’s recommended to add custom code in a plugin or modify your theme’s functions.php
file for easy management. Either option works for this tutorial.
Step 3: Create the Order Creation Function
Start by creating a custom function to programmatically create WooCommerce orders:
// In your custom plugin or functions.php function create_order_programmatically() { // Add code to create the order }
Step 4: Add Products to the Order
Use the wc_create_order()
function to add products to the new order:
// In create_order_programmatically() function $order = wc_create_order(); $items = array( array( 'product_id' => 3, 'quantity' => 1 ), array( 'product_id' => 4, 'quantity' => 2 ), ); foreach ($items as $item) { $product_id = $item['product_id']; $quantity = $item['quantity']; // Add products to order }
Step 5: Add Extra Fees
If you need to add extra fees, use the WC_Order_Item_Fee
class:
// In create_order_programmatically() function $fee = new WC_Order_Item_Fee(); $fee->set_props(array( 'name' => 'Handling Fee', 'amount' => 15.00, 'tax_class' => '', )); $fee->save(); $order->add_item($fee);
Step 6: Set Shipping
Add shipping charges with the WC_Order_Item_Shipping
class:
// In create_order_programmatically() function $shipping = new WC_Order_Item_Shipping(); $shipping->set_method_title('Express Shipping'); $shipping->set_total(8.00); $shipping->save(); $order->add_item($shipping);
Step 7: Apply Coupons for Discounts
Apply coupons for discounts using apply_coupon()
:
// In create_order_programmatically() function $coupon_code = 'WINTER2023'; $coupon = new WC_Coupon($coupon_code); $order->apply_coupon($coupon);
Step 8: Add Customer Information
Fill in the customer’s billing and shipping information:
// In create_order_programmatically() function $customer_info = array( 'first_name' => 'Alice', 'last_name' => 'Johnson', 'email' => 'alice.johnson@example.com', ); $order->set_customer_id(0); $order->set_address($customer_info, 'billing'); $order->set_address($customer_info, 'shipping');
Step 9: Set the Payment Method
Choose the payment method you prefer:
// In create_order_programmatically() function $payment_method = 'paypal'; // PayPal $order->set_payment_method($payment_method);
Step 10: Set Order Status
Set the order’s status using the set_status()
method:
// In create_order_programmatically() function $order_status = 'completed'; $order->set_status($order_status);
Step 11: Save the Order
Save the order:
// In create_order_programmatically() function $order->save(); return $order->get_id();
Step 12: Initiate the Order
Trigger the order creation when necessary:
// In your custom plugin or functions.php function initiate_order_creation() { create_order_programmatically(); // Handle redirection or show success message } add_action('init', 'initiate_order_creation');
Conclusion
By following this guide, you can automate WooCommerce order creation for your store.
External Resources: