How to Manage Orders in WooCommerce in WordPress Easily
To manage orders in WooCommerce on WordPress, use the
WooCommerce Orders section in the WordPress dashboard to view and update orders. You can also programmatically manage orders using WooCommerce's WC_Order class and its methods to update status, add notes, or retrieve order details.Syntax
WooCommerce provides the WC_Order class to work with orders programmatically. You can create an order object by passing the order ID, then use its methods to manage the order.
$order = wc_get_order($order_id);- Gets the order object.$order->update_status('completed');- Changes the order status.$order->add_order_note('Note text');- Adds a note to the order.$order->get_items();- Retrieves items in the order.
php
<?php // Get order by ID $order = wc_get_order(123); // Replace 123 with your order ID // Update order status $order->update_status('completed'); // Add a note to the order $order->add_order_note('Order processed and shipped.'); // Get order items $items = $order->get_items(); foreach ($items as $item) { echo $item->get_name() . '\n'; } ?>
Example
This example shows how to programmatically change an order status to 'completed' and add a note to it in WooCommerce.
php
<?php function complete_order_and_add_note($order_id) { $order = wc_get_order($order_id); if (!$order) { return 'Order not found.'; } // Change status to completed $order->update_status('completed'); // Add a note $order->add_order_note('Order marked as completed programmatically.'); return 'Order updated successfully.'; } // Example usage echo complete_order_and_add_note(123); // Replace 123 with your order ID ?>
Output
Order updated successfully.
Common Pitfalls
Common mistakes when managing WooCommerce orders include:
- Using incorrect order IDs or not checking if the order exists before updating.
- Not using the correct order status slug (e.g., 'completed', 'processing').
- Forgetting to save changes or call update methods properly.
- Trying to modify orders without proper permissions or hooks.
Always validate the order object before making changes.
php
<?php // Wrong way: Not checking if order exists $order = wc_get_order(999999); // Non-existent order if ($order) { $order->update_status('completed'); } else { echo 'Order not found.'; } // Right way: Check before updating $order = wc_get_order(999999); if ($order) { $order->update_status('completed'); } else { echo 'Order not found.'; } ?>
Quick Reference
| Action | Method | Description |
|---|---|---|
| Get order object | wc_get_order($order_id) | Retrieve order by ID |
| Update order status | $order->update_status('status') | Change order status (e.g., 'completed') |
| Add order note | $order->add_order_note('text') | Add a note visible in admin |
| Get order items | $order->get_items() | Get products in the order |
| Get order total | $order->get_total() | Retrieve total amount |
Key Takeaways
Use wc_get_order($order_id) to get the order object before managing it.
Update order status with $order->update_status('status-slug') using valid status slugs.
Add notes to orders with $order->add_order_note() for admin tracking.
Always check if the order exists to avoid errors.
Use WooCommerce dashboard for manual order management and the WC_Order class for programmatic control.