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?

To encourage customers to complete an order and to encourage repeat purchases in your store, this customization will be helpful in such scenarios. At times, it can be a surprise to your customers with an unexpected free gift in their completed order. 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' );





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.

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

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