Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add Prefix Only for Completed Orders in WooCommerce?

Adding prefix such as ‘PAID’ to a specific order status makes it immediately clear that the order has been paid for and is complete. By incorporating such a prefix, store owners can quickly identify paid orders amidst a list of orders available in the WooCommerce Orders table. Let’s dive in to see how this WooCommerce customization adds a prefix ‘PAID’ to your completed orders!

Solution: Add Prefix Only for Paid Orders in WooCommerce

The code concatenates “PAID-” with a unique identifier created by uniqid() for specific order statuses that goes to ‘completed’ status.

function ts_change_order_id_after_payment_completion( $order_id ) {
    // Get the order object
    $order = wc_get_order( $order_id );

    // Check if the order status is 'completed'
    if ( $order->get_status() === 'completed' ) {
        // Generate a new order ID based on your custom logic
        $new_order_id = ts_generate_custom_order_id();

        // Update the order ID
        $order->set_order_key( wc_clean( $new_order_id ) );
        $order->set_id( null );
        $order->save();
    }
}

// Hook the function to run after payment completion, but only for completed orders
add_action( 'woocommerce_order_status_completed', 'ts_change_order_id_after_payment_completion', 10, 1 );

// Define a function to generate a custom order ID
function ts_generate_custom_order_id() {
    // Example logic: Concatenate a prefix with the current time
    $custom_order_id = 'PAID-' . uniqid(); // Modified prefix to 'PAID-' and added a unique identifier

    return $custom_order_id;
}

// Modify the order number using the woocommerce_order_number filter hook
function ts_modify_order_number( $order_id ) {
    // Check if the order status is 'completed'
    $order = wc_get_order( $order_id );
    if ( $order && $order->get_status() === 'completed' ) {
        // Generate a new order ID based on your custom logic
        $new_order_id = ts_generate_custom_order_id();

        return $new_order_id;
    }

    return $order_id; // Return the original order ID if not completed
}

// Hook the function to modify the order number, but only for completed orders
add_filter( 'woocommerce_order_number', 'ts_modify_order_number', 10, 1 );

Output

When a customer completes a purchase and the order status changes to ‘completed’, only such order IDs will be added with the prefix ‘PAID’.

How to Add Prefix Only for Completed Orders in WooCommerce? - Tyche Softwares

Here, we have targeted specific order statuses and added a custom order number with a prefix. Instead, you can also add category names as prefix and suffix to order numbers. This helps you categorize and organize orders based on product types, making it easier to track and manage them.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x