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?

Offering BOGO “Buy One Get One” deals based on the billing/shipping country allows store owners to align their promotions with customer locality-based preferences. 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;
            }
        }
    }
}

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.

BOGO Gifts Based on Customer's Billing/Shipping Country 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

In the same way, you can also provide BOGO (Buy One Get One) gifts based on the selected payment method in WooCommerce.

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