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.
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.
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.