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

How to Add BOGO (Buy One Get One) Gifts Based on Customer’s Billing/Shipping Country in WooCommerce?

This strategy of offering BOGO “Buy One Get One” gifts based on the billing/shipping country allows store owners to align their promotions with customer locality-based preferences. For instance, if your store sells skin care products worldwide, then you can target customers in sunny regions and provide sunscreen as gift products.

This customization will help you to tailor your marketing promotions to meet customers’ preferences or the trends in their locality.

Solution: Add BOGO (Buy One Get One) Gifts Based on Customer’s Billing/Shipping Country

This code snippet will allow you to add free gifts only for the targeted country ‘US’ and removes the free product if any other country is selected.

// Add a free gift based on shipping country
add_action('woocommerce_before_calculate_totals', 'ts_add_free_gift_based_on_shipping_country');

function ts_add_free_gift_based_on_shipping_country($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // Define the target shipping country code
    $target_country_code = 'US'; // Change this to your desired country code

    // Get the current shipping country
    $shipping_country = WC()->customer->get_shipping_country();

    // Check if the shipping country matches the target country
    if ($shipping_country === $target_country_code) {
        // Get the free gift product ID
        $free_gift_product_id = 470; // Change this to the actual product ID of your free gift

        // Check if the free gift is not already in the cart
        $free_gift_in_cart = false;
        foreach ($cart->get_cart() as $cart_item) {
            if ($cart_item['product_id'] == $free_gift_product_id) {
                $free_gift_in_cart = true;
                break;
            }
        }

        // If the free gift is not in the cart, add it
        if (!$free_gift_in_cart) {
            $cart->add_to_cart($free_gift_product_id);
        }
    }
}

// Remove the free gift based on shipping country change
add_action('woocommerce_before_calculate_totals', 'ts_remove_free_gift_based_on_shipping_country');

function ts_remove_free_gift_based_on_shipping_country($cart) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    // Define the target shipping country code
    $target_country_code = 'US'; // Change this to your desired country code

    // Get the current shipping country
    $shipping_country = WC()->customer->get_shipping_country();

    // Check if the shipping country does not match the target country
    if ($shipping_country !== $target_country_code) {
        // Get the free gift product ID
        $free_gift_product_id = 470; // Change this to the actual product ID of your free gift

        // Check if the free gift is in the cart, and remove it
        foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
            if ($cart_item['product_id'] == $free_gift_product_id) {
                $cart->remove_cart_item($cart_item_key);
                break;
            }
        }
    }
}
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

When the customer selects the billing or shipping country that matches the target country code (‘US’), the code will add the free gift product to the checkout page.

adding BOGO gifts for various billing and shipping countries in WooCommerce.

If any other country is selected other than ‘US’ then the code tends to remove the added free product from the checkout page.

BOGO Gifts Based on Customer's Billing/Shipping Country in WooCommerce

You may have noticed the recent trend of customers preferring certain payment methods over others. Similar to the strategy as illustrated in this post, you can target BOGO (Buy One Get One) promotions based on the selected payment method in WooCommerce and reward your customers.

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