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

 How to Add a BOGO (Buy One Get One) Offer on Order Status Change in WooCommerce?

Offering a free product or a BOGO deal during a specific order status change can encourage them to be repeat customers. This code is designed to automatically add a free product to an order when the order status is changed to ‘completed’ and customize the content of the customer’s completed order email to display information about any added free gift.

function order_has_specific_product( $product_id, $order ) {
    // Loop through order items to check if a product is on the current order
    foreach ( $order->get_items() as $item ) {
        if ( in_array( $product_id, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            return true;
        }
    }
    return false;
}

add_action( 'woocommerce_order_status_changed', 'ts_add_free_product_on_order_enquiry_status', 20, 4 );
function ts_add_free_product_on_order_enquiry_status( $order_id, $old_status, $new_status, $order ){
    $product_id = '470'; // Free product to be added only once

    if ( "completed" === $new_status && ! order_has_specific_product( $product_id, $order ) ) {
        $product = wc_get_product( $product_id );
        $order->add_product( $product );
        $order->calculate_totals(); // calculate taxes, totals and save (method includes save() method)
    }
}

Output

By default, when the customer places an order the order status gets changed to ‘on-hold’ in WooCommerce. As mentioned in the code, we will offer the free gift only when the status gets changed from ‘on-hold’ to ‘completed’ status.

 How to Add a BOGO (Buy One Get One) Offer on Order Status Change in WooCommerce? - Tyche Softwares

But when the order status is ‘on-hold’ the gift doesn’t get added to the order details/thank you page and on the admin order details page.

 How to Add a BOGO (Buy One Get One) Offer on Order Status Change in WooCommerce? - Tyche Softwares
 How to Add a BOGO (Buy One Get One) Offer on Order Status Change in WooCommerce? - Tyche Softwares

As we saw above, you can add free products based on different parameters. Refer to this post that helps you add a product to your WooCommerce cart in 6 different scenarios such as based on cart total, categories, and on website visit, etc.

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

Share It:

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