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

How to Maximize BOGO Offers by Offering Multiple Free Items in WooCommerce?

This advanced customization combines the power of Buy One, Get One (BOGO) offers with the option for customers to select free samples of their choice during the purchase. The code offers the option to choose multiple free products through checkbox selections on the individual product pages. Additionally, it ensures that customers can get access to these free samples only if they add the main product to the cart or else it pops up a notice message.

add_action('woocommerce_single_product_summary', 'ts_add_free_sample_add_cart', 35);

function ts_add_free_sample_add_cart() {
    global $product;

    ?>
    <form class="cart" method="post" enctype='multipart/form-data'>
        <fieldset>
            <legend>Select Free Products:</legend>
            <?php
            $main_product_id = $product->get_id();
            $free_product_ids = array('470', '457', '100'); // Replace with your actual product IDs

            foreach ($free_product_ids as $free_product_id) {
                $free_product = wc_get_product($free_product_id);
                ?>
                <label>
                    <input type="checkbox" name="free_samples[]" value="<?php echo esc_attr($free_product_id); ?>">
                    <?php echo esc_html($free_product->get_name()); ?>
                </label><br>
                <?php
            }
            ?>
        </fieldset>
        <input type="hidden" name="add-to-cart" value="order_free_products">
        <input type="hidden" name="main_product_id" value="<?php echo esc_attr($main_product_id); ?>">
        <input type="hidden" name="additional_value1" value="470">
        <input type="hidden" name="additional_value2" value="457">
        <input type="hidden" name="additional_value3" value="100">
        <button type="submit" class="single_add_to_cart_button button alt" style="background-color: #FFBF43">Order Free Products</button>
    </form>
    <?php
}

add_action('init', 'ts_process_free_samples_form_submission');

function ts_process_free_samples_form_submission() {
    if (isset($_POST['add-to-cart'])) {
        $main_product_id = isset($_POST['main_product_id']) ? $_POST['main_product_id'] : '';
        $free_samples = isset($_POST['free_samples']) ? $_POST['free_samples'] : array();
        $additional_value1 = isset($_POST['additional_value1']) ? $_POST['additional_value1'] : '';
        $additional_value2 = isset($_POST['additional_value2']) ? $_POST['additional_value2'] : '';
        $additional_value3 = isset($_POST['additional_value3']) ? $_POST['additional_value3'] : '';

        // Check if the main product is in the cart
        if (!empty($main_product_id) && !is_product_in_cart($main_product_id)) {
            // Check if the notice has already been added
            if (!wc_notice_count('error')) {
                wc_add_notice(__('Please add the main product to your cart to get access to these free products.', 'your-text-domain'), 'error');
            }
            return;
        }

        // Now you can use these values as needed
        // For example, you can access $additional_value1, $additional_value2, $additional_value3 here

        if (!empty($free_samples)) {
            foreach ($free_samples as $free_sample_id) {
                $product = wc_get_product($free_sample_id);

                if ($product) {
                    WC()->cart->add_to_cart($free_sample_id);
                }
            }
        }
    }
}

add_filter('woocommerce_cart_item_name', 'ts_alter_cart_item_name', 10, 3);

function ts_alter_cart_item_name($product_name, $cart_item, $cart_item_key) {
    if (isset($cart_item['free_samples'])) {
        foreach ($cart_item['free_samples'] as $free_sample_id) {
            if ($free_sample_id == $cart_item['product_id']) {
                $product = wc_get_product($free_sample_id);
                return 'Ordered: ' . $product->get_name();
            }
        }
    }
    return $product_name;
}

// Helper function to check if a product is in the cart
function is_product_in_cart($product_id) {
    foreach (WC()->cart->get_cart() as $cart_item) {
        if ($product_id == $cart_item['product_id']) {
            return true;
        }
    }
    return false;
}

Output

On individual product pages, customers will be provided with checkboxes displaying available complimentary gifts. Once the main product is added to the cart, customers can then select their preferred free products and proceed by clicking the ‘Order Free Products’ button.

How to Maximize BOGO Offers by Offering Multiple Free Items in WooCommerce? - Tyche Softwares

If the customer tries to get access to these free products without adding the main product to the cart, then the notice message is displayed as shown below that will also restrict these products from being automatically added to the cart.

How to Maximize BOGO Offers by Offering Multiple Free Items in WooCommerce? - Tyche Softwares

When the customer adds the main product, subsequently followed by adding the free products, then the selected free products gets added to the cart as shown below.

How to Maximize BOGO Offers by Offering Multiple Free Items in WooCommerce? - Tyche Softwares

Alternatively, you can also apply bulk quantity discount (Buy 10 Get 1 Free) 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