How to Create Order Programmatically in WordPress

Creating orders programmatically in WordPress using WooCommerce can streamline your e-commerce site by automating order processing, integrating with third-party systems and setting up custom workflows. This comprehensive guide will cover everything you need to know about programmatically creating orders, including setting order status, adding coupon codes, managing customer information and working with metadata.

Introduction

This blog post covers the essentials of creating orders programmatically in WordPress using WooCommerce providing you with the tools to automate processes and customize workflows efficiently.

Understanding WooCommerce Orders

WooCommerce orders are a custom post type (shop_order) with various meta fields that store order details such as customer information, order status and items purchased. Understanding this structure is key to manipulating orders programmatically.

Setting Up Your Development Environment

  1. Install and Activate WooCommerce: Ensure WooCommerce is installed and activated on your WordPress site.
  2. Create a Child Theme: Create and activate a child theme to safely add custom code.
  3. Set Up a Custom Plugin: Alternatively, create a custom plugin to write the order creation code.

Creating an Order Programmatically

To create an order programmatically you’ll need to use WooCommerce’s WC_Order class. Here’s a step-by-step guide:

1. Initialize the Order

First initialize a new order object:

$order = wc_create_order();

Adding Order Items

2. Add Products to the Order

Add products to the order by specifying the product ID and quantity:

$product_id = 123; // Replace with your product ID
$quantity = 2;

$order->add_product(get_product($product_id), $quantity);

Setting Order Status

3. Set Order Status

WooCommerce order statuses include:

  • pending: Payment pending.
  • processing: Payment received, order being processed.
  • on-hold: Awaiting payment.
  • completed: Order fulfilled and complete.
  • cancelled: Order cancelled by admin or customer.
  • refunded: Order refunded.
  • failed: Payment failed.

Set the order status:

$order->set_status('processing');

Adding Coupon Codes

4. Apply Coupon Codes

To apply a coupon code to the order you need to use the apply_coupon method:

$coupon_code = 'DISCOUNT10'; // Replace with your coupon code
$order->apply_coupon($coupon_code);
$order->calculate_totals();

Managing Customer Information

5. Set Customer Details

Set the customer’s billing and shipping details:

$billing_address = array(
    'first_name' => 'Jason',
    'last_name'  => 'Statham',
    'company'    => '',
    'email'      => 'jason@example.com',
    'phone'      => '123456789',
    'address_1'  => '123 Main St',
    'address_2'  => '',
    'city'       => 'Anytown',
    'state'      => 'CA',
    'postcode'   => '12345',
    'country'    => 'US'
);

$order->set_address($billing_address, 'billing');
$order->set_address($billing_address, 'shipping');

Adding Custom Metadata

6. Add Custom Metadata

To add custom metadata to an order use the update_meta_data method of the WC_Order class:

$order->update_meta_data('_custom_meta_key', 'custom_meta_value');
$order->save();

Calculating Totals and Saving the Order

7. Calculate Totals

After adding products and applying coupons, you need to calculate the order totals, including taxes and shipping if applicable:

$order->calculate_totals();

8. Save the Order

Finally save the order to ensure all details are stored:

$order->save();

Testing and Debugging

  1. Test in a Staging Environment: Always test your code in a staging environment before deploying to a live site.
  2. Debugging: Use error_log() to log information for debugging purposes.
  3. WooCommerce Logs: Check WooCommerce logs for any errors related to order creation.

Conclusion

Creating orders programmatically in WooCommerce can significantly streamline your e-commerce processes making it easier to automate tasks and integrate with other systems. By following this guide you can efficiently create orders, apply coupon codes, manage customer information and add custom metadata.

Feel free to expand this functionality by adding more complex logic, integrating with APIs or customizing the order creation process further. Happy coding!

Leave a Reply

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