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

How to Add a Free Gift to All Completed Order Email Notifications in WooCommerce?

Surprising customers with an unexpected free gift when they complete an order not only encourages customers to make a purchase but to become repeat customers of your store. This customization isn’t just about adding a product to a cart—it’s about notifying the customer of the free gift via email and giving a touch of joy in their shopping experience.

Solution: Add a Free Gift to All Completed Order Email Notifications in WooCommerce

This code will automatically add a free gift to email notifications of completed orders.

// Add action to include added free gift in completed order email
add_action('woocommerce_email_after_order_table', 'ts_email_after_order_table', 10, 4);
function ts_email_after_order_table($order, $sent_to_admin, $plain_text, $email)
{
    // Check if the email is for completed order status
    if ('customer_completed_order' === $email->id) {
        $free_gift_product_id = '470'; // Replace with your actual free gift product ID

        // Check if the free gift is in the order
        $free_gift_added = false;
        foreach ($order->get_items() as $item) {
            if ($item->get_product_id() == $free_gift_product_id) {
                $free_gift_added = true;
                break;
            }
        }

        // If the free gift is not in the order, add it
        if (!$free_gift_added) {
            $free_gift_quantity = 1; // You can adjust the quantity as needed
            $order->add_product(wc_get_product($free_gift_product_id), $free_gift_quantity);
        }

        // Output information about the added free gift in the email
        echo '<p>Free Gift Added:</p>';
        echo '<table cellspacing="0" cellpadding="6" style="width: 100%; color: #636363; border: 1px solid #e5e5e5;" border="1"><tbody>';
        echo '<tr><th style="text-align: left;">Product</th><th style="text-align: left;">Quantity</th></tr>';

        // Output free gift product details
        foreach ($order->get_items() as $item) {
            if ($item->get_product_id() == $free_gift_product_id) {
                echo '<tr>';
                echo '<td>' . $item->get_name() . '</td>';
                echo '<td>' . $item->get_quantity() . '</td>';
                echo '</tr>';
                break; // No need to iterate further once the free gift is found
            }
        }

        echo '</tbody></table>';
    }
}

// Add a notice message on the cart page
function add_free_gift_notice() {
    wc_print_notice( 'Complete your order now and receive a free gift!', 'notice' );
}
add_action( 'woocommerce_before_cart', 'add_free_gift_notice' );

flexi bogo cta banner image


This to the shop owners who are running or planning to run BOGO offers on their WooCommerce store…

BOGO deals are great for increasing your sales, but have you thought about which offers are bringing you more revenue and which offers are not performing that great?

Don’t just set a BOGO deal, track the revenue generated by your deals in real-time with the Flexi BOGO for WooCommerce plugin.

Output

After the order is successfully placed by the customer, the free product details are automatically sent to the completed order of email notifications as shown below.

adding free gifts to completed order email notifications in WooCommerce

The last function in the code will display a notice message on the cart page to inform customers about the free gift in order to complete the purchase.

How to Add a Free Gift to All Completed Order Email Notifications in WooCommerce? - Tyche Softwares

Similarly, you can also segment such bogo offers based on customer email domain and target personalized offers based on their purchasing behaviors.

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