Integrating WooCommerce License Manager API v3 with Your WordPress Plugin: A Step-by-Step Guide
August 4, 2024How to Get a Free SSL Certificate for Your WordPress Site
August 7, 2024If you run an online store selling digital products like software licenses, automating your order fulfillment process can significantly enhance your efficiency and customer satisfaction. In this tutorial, we’ll walk you through how to automatically mark orders as complete in WooCommerce when all ordered items have corresponding license keys available in the database. Let’s get started!
Why Automate Order Completion?
Automating the order completion process offers several benefits:
- Improved Customer Experience: Customers receive their products faster, which enhances their shopping experience and reduces wait times.
- Operational Efficiency: Automating routine tasks frees up your time to focus on more critical aspects of your business.
- Error Reduction: Automation minimizes the risk of human error, ensuring that orders are processed accurately.
Prerequisites
Before we begin, make sure you have the following:
- A WooCommerce store with WooCommerce License Manager installed and activated.
- Access to your theme’s
functions.php
file.
Step-by-Step Guide
Step 1: Add the Code to Your Theme’s functions.php File
Add the following code to your theme’s functions.php
file. This code will automatically mark orders as complete if all items in the order have available license keys.
function wclm_auto_complete_order_if_all_license_keys_available( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$items = $order->get_items();
$all_keys_available = true;
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
// Check if license keys are available for the product or if the order item already has a license key assigned
if ( ! wclm_has_license_keys_available( $product_id, $variation_id, $order_id ) ) {
$all_keys_available = false;
break;
}
}
if ( $all_keys_available ) {
$order->update_status( 'completed' );
}
}
function wclm_has_license_keys_available( $product_id, $variation_id, $order_id ) {
global $wpdb;
$table_name = $wpdb->prefix . 'wc_fs_product_licenses_keys';
// Check if the order item already has a license key assigned
$result = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE product_id = %d AND variation_id = %d AND order_id = %d",
$product_id, $variation_id, $order_id
) );
return $result > 0;
}
add_action( 'woocommerce_thankyou', 'wclm_auto_complete_order_if_all_license_keys_available' );
Step 2: Test Your Implementation
- Place an Order: Add products to your cart and place an order.
- Check the Order Status: Verify that the order status changes to “Completed” if all items have available license keys.
Step 3: Troubleshooting
- License Key Availability: Ensure you have license keys added to the license manager and that license keys are marked as available.
Conclusion
By following this guide, you can automate the order completion process in your WooCommerce store, ensuring a smoother and more efficient workflow for your digital products. This automation not only improves the customer experience but also allows you to focus on growing your business.
For more WooCommerce tips and tricks, stay tuned to our blog!