To programmatically create WooCommerce orders in WordPress, you can use the wc_create_order()
function. This enables you to add products, fees, shipping, and even apply coupons. Here’s a comprehensive guide to walk you through the entire process.
Step 1: Install WordPress and WooCommerce
Make sure WordPress and the WooCommerce plugin are installed and activated on your site. You can install WooCommerce directly from the WordPress dashboard or download it from the plugin repository.
Step 2: Add Code in Custom Plugin or functions.php
It’s best practice to add your custom code in a custom plugin or your theme’s functions.php
file. Either create a plugin or edit your functions.php
file.
Step 3: Define the Order Creation Function
Define a custom function to handle the WooCommerce order creation process. Here’s how to get started:
// In your custom plugin or functions.php function create_woocommerce_order() { // Add order creation code here }
Step 4: Add Items to the Order
You can add products to the order by using the wc_create_order()
function:
// 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 each product to the order }
Step 5: Add Fees
Add a fee to the order using the WC_Order_Item_Fee
class:
// In create_woocommerce_order() function $fee = new WC_Order_Item_Fee(); $fee->set_props(array( 'name' => 'Extra Charge', 'amount' => 12.00, 'tax_class' => '', )); $fee->save(); $order->add_item($fee);
Step 6: Add Shipping
For shipping, utilize the WC_Order_Item_Shipping
class:
// In create_woocommerce_order() function $shipping_item = new WC_Order_Item_Shipping(); $shipping_item->set_method_title('Standard Shipping'); $shipping_item->set_total(5.00); $shipping_item->save(); $order->add_item($shipping_item);
Step 7: Apply Coupons
If you wish to add discounts, use the apply_coupon()
method:
// In create_woocommerce_order() function $coupon_code = 'SPRING2023'; $coupon = new WC_Coupon($coupon_code); $order->apply_coupon($coupon);
Step 8: Set Customer Billing & Shipping Details
Provide billing and shipping info for the customer:
// In create_woocommerce_order() function $customer_data = array( 'first_name' => 'Jane', 'last_name' => 'Smith', 'email' => 'jane.smith@example.com', ); $order->set_customer_id(0); $order->set_address($customer_data, 'billing'); $order->set_address($customer_data, 'shipping');
Step 9: Set Payment Method
Choose the payment method for the order:
// In create_woocommerce_order() function $payment_method = 'cod'; // Cash on Delivery $order->set_payment_method($payment_method);
Step 10: Set Order Status
Define the order status with the set_status()
method:
// In create_woocommerce_order() function $order_status = 'completed'; $order->set_status($order_status);
Step 11: Save the Order
Finally, save the order:
// In create_woocommerce_order() function $order->save(); return $order->get_id();
Step 12: Trigger the Order
Create a trigger for order creation:
// In your custom plugin or functions.php function trigger_order_creation() { create_woocommerce_order(); // Redirect or display success } add_action('init', 'trigger_order_creation');
Conclusion
By following this simple guide, you can create WooCommerce orders programmatically to streamline your process.
External Resources: