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 WooCommerce Custom Order Status Change?

Though WooCommerce provides you with a set of default order statuses such as “Processing”, “On Hold”, “Completed”, and “Cancelled” these might not be enough for you to control and manage the flow of orders. In such cases, you might use plugins such as custom order status for WooCommerce that will allow you to create custom statuses like “Dispatched”, “Scheduled”, “Refunded”, “Shipped” or anything you like according to your convenience and requirement.

The code snippet given in this post will help to add BOGO offers for selected custom order status. For e.g whenever an order gets changed to ‘scheduled’ status the free gift will also be added.

To begin with the implementation steps, you need to download the custom order status for WooCommerce plugin and configure some basic settings as given below.

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

When clicking the ‘custom order statuses page’ you will be directed to a page from where the custom order statuses can be created. Click on ‘Add new ‘ and create your preferred custom status as shown below.

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

Here’s how you can manage the newly created custom order statuses using the bulk actions field on the admin orders section page.

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

As a next step, you can add the given code in the code snippets plugin, save, and activate the snippet. The code helps to offer a free gift for all orders that are ‘scheduled’ as this specific status is defined in the code.

When the store owners are utilizing an order delivery date for WooCommerce plugin that will allow customers to schedule their delivery date and time, you can use such scheduled order status. If not, based on your business needs any other custom status such as ‘dispatched’ or ‘paid’ can be used. When the order status of orders gets transitioned to the ‘scheduled’ status, the code automatically includes a complimentary add-on to the order.

Note: You can modify the code by changing the slug of any custom order status to make it work the same for other custom order status.

// Function to add the free gift for the 'scheduled' status
function ts_add_free_gift_on_scheduled_status( $order_id ) {
    // Check if the current post type is 'shop_order'
    if ( 'shop_order' === get_post_type( $order_id ) ) {
        // Get the order
        $order = wc_get_order( $order_id );

        $product_id = '470'; // Replace with the actual product ID of the free gift

        // Check if the order status is 'scheduled' and the free product has not been added yet
        if ( 'scheduled' === $order->get_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)
        }
    }
}

// Hook to add the free gift when order status is changed to 'scheduled'
add_action( 'woocommerce_order_status_changed', 'ts_add_free_gift_on_scheduled_status', 10, 1 );

// Function to check if a specific product is in the order
function order_has_specific_product( $product_id, $order ) {
    // Loop through order items to check if a product is in 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;
}

Output

When orders are scheduled on a specific date, then those orders can be set the custom order status as ‘scheduled’ as shown below.

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

The following output shows the free gift has been added only for orders with the ‘scheduled’ status.

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

Additional customization can be done by using the custom order status for WooCommerce plugin to send custom notification emails & SMS when the status of an order is changed to a custom status.

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