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

How to Exclude Taxes for BOGO “Buy One Get One” Promoted Products in WooCommerce?

While running BOGO offers or a a promotional “Buy one Get one Free” offers in your online store, you can also provide additional tax exemption benefits to discounted products. The code implements a “Buy one Get one Free” promotion for specific targeted products in the cart. When items of these targeted products are added to cart, a free item is offered. Additionally, it excludes tax for all products in the cart if these discounted items are present. And later on if these products are removed from the cart, the taxes are applied for other products.

add_action('template_redirect', 'ts_modify_cart_for_bogo_and_tax_exemption');

function ts_modify_cart_for_bogo_and_tax_exemption() {
    if (is_admin()) return;
    if (WC()->cart->is_empty()) return;

    $main_product_ids = array(100, 457); // Add your specified main product IDs
    $product_gifted_id = 470;

    // Check if any main product is in the cart
    $main_product_in_cart = false;
    foreach ($main_product_ids as $main_product_id) {
        $main_product_cart_id = WC()->cart->generate_cart_id($main_product_id);
        if (WC()->cart->find_product_in_cart($main_product_cart_id)) {
            $main_product_in_cart = true;
            break;
        }
    }

    // Check if gift id is in cart
    $product_gifted_cart_id = WC()->cart->generate_cart_id($product_gifted_id);
    $product_gifted_in_cart = WC()->cart->find_product_in_cart($product_gifted_cart_id);

    // If any main product is in cart, add the gift and exempt taxes
    if ($main_product_in_cart) {
        if (!$product_gifted_in_cart) {
            WC()->cart->add_to_cart($product_gifted_id);

            // Add tax exemption logic for the gifted product
            ts_exempt_tax_for_product($product_gifted_id);
        }
    } else {
        // If no main product is in cart, remove the gift and remove tax exemption
        if ($product_gifted_in_cart) {
            WC()->cart->remove_cart_item($product_gifted_in_cart);
            ts_remove_tax_exemption();
        }
    }
}

function ts_exempt_tax_for_product($product_id) {
    $vat_exempt = WC()->customer->is_vat_exempt();

    // Exempt tax for the specified product if not already exempt
    if (!$vat_exempt) {
        WC()->customer->set_is_vat_exempt(true); // Set customer tax exempt
        wc_clear_notices();
        wc_add_notice(__('Taxes excluded for the gifted product and specified main products.'), 'success');
    }
}

function ts_remove_tax_exemption() {
    // Remove tax exemption for the customer
    WC()->customer->set_is_vat_exempt(false);
    wc_clear_notices();
    wc_add_notice(__('Tax exemption removed for the gifted product and specified main products.'), 'error');
}

Output

When the customer adds the products to the cart and if any of the products matches the targeted product ID as defined in the code, then in this case the taxes will not be applied to any products in the cart.

How to Exclude Taxes for BOGO "Buy One Get One" Promoted Products in WooCommerce? - Tyche Softwares

And in case where the targeted product is removed from the cart, taxes will be applied to the rest of the products in the cart.

How to Exclude Taxes for BOGO "Buy One Get One" Promoted Products in WooCommerce? - Tyche Softwares

In similar to the above customization, you can provide BOGO offers based on different conditions such as applying bogo buy one get one discounts to cart’s cheapest item during a promotion 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