How to Disable WooCommerce Draft Orders: A Step-by-Step Guide
If you’ve been using WooCommerce’s Checkout Block, you’ve probably noticed that it creates Draft orders (checkout-draft
) as soon as a customer lands on the checkout page. While these draft orders are temporary and are automatically cleaned up every 24 hours, many store owners have been asking: “Can I prevent these draft orders from being created in the first place?”
The short answer is: Not directly. WooCommerce doesn’t currently provide a built-in way to stop draft orders from being created. However, we’ve got a workaround! Instead of preventing the creation of draft orders, we can immediately delete them as soon as they’re created. This ensures that draft orders don’t clutter your Orders page or database.
In this post, We’ll walk you through a simple snippet that does exactly that. Whether you’re using the traditional order storage method or the new High-Performance Order Storage (HPOS), this solution does the trick!
Why Delete Draft Orders Immediately?
Draft orders are created by the Checkout Block to temporarily hold customer information (like cart items, shipping details, and addresses) while they complete the checkout process. However, if a customer abandons the checkout, these draft orders can pile up, even though they’re eventually cleaned up by WooCommerce’s daily cron job.
By deleting draft orders immediately, you can:
- Keep your Orders page clean: No more unnecessary draft orders cluttering your admin area.
- Reduce database bloat: Draft orders won’t linger in your database, even temporarily.
- Improve performance: Fewer orders in the database mean faster queries and a smoother backend experience.
The Solution: Delete Draft Orders on Creation
Here’s a snippet that deletes draft orders as soon as they’re created. It works with both the traditional order storage method and HPOS, so you don’t have to worry about compatibility.
<?php
/**
* Automatically Delete WooCommerce Draft Orders Immediately After Creation.
*
* This snippet immediately deletes draft orders as soon as they are created
* It also includes a fallback to delete any remaining draft orders during the
* shutdown phase.
*
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
/**
* Immediately delete any shop order with a draft status as soon as it’s inserted.
*/
add_action('wp_insert_post', 'idod_delete_draft_order_on_insert', 0, 3);
function idod_delete_draft_order_on_insert($post_id, $post, $update) {
// Only target shop orders.
if ('shop_order' !== $post->post_type) {
return;
}
// Check if the order status is a draft.
$status = get_post_status($post_id);
if (in_array($status, ['checkout-draft', 'wc-checkout-draft', 'auto-draft'], true)) {
wp_delete_post($post_id, true); // Force delete.
}
}
/**
* Fallback: On shutdown, delete any remaining orders with a draft status.
*/
add_action('shutdown', 'idod_delete_remaining_draft_orders');
function idod_delete_remaining_draft_orders() {
// Query all remaining draft orders.
$args = array(
'status' => ['checkout-draft', 'wc-checkout-draft', 'auto-draft'],
'limit' => -1,
);
$orders = wc_get_orders($args);
// Delete each draft order.
if (!empty($orders)) {
foreach ($orders as $order) {
error_log("Shutdown Draft Deletion: Deleting order " . $order->get_id() . " (status: " . $order->get_status() . ").");
$order->delete(true); // Force delete.
}
}
}
How It Works
- Immediate Deletion:
- The
wp_insert_post
hook catches draft orders as soon as they’re created.
- If the order status is
checkout-draft
, wc-checkout-draft
, or auto-draft
, it’s immediately deleted using wp_delete_post()
.
- Fallback Mechanism:
- The
shutdown
hook ensures that any remaining draft orders are deleted at the end of the request.
- This acts as a safety net in case the immediate deletion fails.
- Logging:
- Each deletion is logged to the PHP error log (
wp-content/debug.log
) for debugging purposes.
How to Use This Snippet
- Add the Code:
- Copy the snippet and paste it into your theme’s
functions.php
file or a custom functionality plugin, like Code Snippets.
- Test It Out:
- Go through the checkout process using the Checkout Block.
- Verify that no draft orders are created or visible in the WooCommerce admin.
- Check the Logs:
- Look at the error log (
wp-content/debug.log
) to confirm that draft orders are being deleted.
Why This Works
- Compatibility: Works with both traditional order storage and HPOS.
- Efficiency: Draft orders are deleted immediately, keeping your database clean.
- Flexibility: The fallback mechanism ensures no draft orders slip through the cracks.
Final Thoughts
While it’s currently not possible to prevent WooCommerce from creating draft orders, this snippet is the next best thing. By deleting draft orders immediately, you can keep your Orders page clean and your database optimized. Plus, it’s super easy to implement!
If you have any questions or run into issues, feel free to drop a comment below.
Share the Love
If you found this snippet helpful, consider sharing it with other WooCommerce store owners. You can also access the snippet on GitHub for easy access:
Get it on GitHub
Let’s make WooCommerce a little cleaner, one draft order at a time! 😊
Leave a Reply