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

How to Add BOGO Gifts When the Site is Accessed from a Mobile Browser?

If we look into recent surveys of online shopping behavior, it is evident that 83 percent of people use their mobile to purchase things online. Also if your store has a mobile app, then offering a free gift can be a strategy to drive users to download and use the app.
This post guides you well to offer gift only when customers are accessing your online shop from a mobile browser.

function ts_add_free_product_on_mobile() {
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            // Check if the user is on a mobile device
            if ($(window).width() < 768) {
                var freeProductId = 470; // Product Id of the free product which will get added to cart
                var found = false;

                // Check if the free product is not already in the cart
                if (typeof wc_cart_params !== 'undefined' && wc_cart_params.cart_contents_count > 0) {
                    $.each(wc_cart_params.cart, function(key, value) {
                        if (value.product_id === freeProductId) {
                            found = true;
                            return false; // Exit the loop
                        }
                    });

                    // If the product is not found, add it to the cart
                    if (!found) {
                        var data = {
                            action: 'ts_add_free_product_to_cart',
                            product_id: freeProductId
                        };

                        $.post(wc_cart_params.ajax_url, data, function(response) {
                           
                        });
                    }
                } else {
                    // If no products in the cart, add the free product
                    var data = {
                        action: 'ts_add_free_product_to_cart',
                        product_id: freeProductId
                    };

                    $.post(wc_cart_params.ajax_url, data, function(response) {
                        
                    });
                }
            }
        });
    </script>
    <?php
}
add_action('wp_footer', 'ts_add_free_product_on_mobile');

// Add the free product to the cart
function ts_add_free_product_to_cart() {
    if (isset($_POST['product_id'])) {
        $product_id = absint($_POST['product_id']);
        WC()->cart->add_to_cart($product_id);
        die();
    }
}
add_action('wp_ajax_ts_add_free_product_to_cart', 'ts_add_free_product_to_cart');
add_action('wp_ajax_nopriv_ts_add_free_product_to_cart', 'ts_add_free_product_to_cart');

Output

When a user on a mobile device visits the site, the code checks if a specific product (identified by freeProductId) is not already in the cart. If not found, it adds the free product to the cart using AJAX requests. The free gift doesn’t get added if accessed from a Desktop/Laptop device.

How to Add BOGO Gifts When the Site is Accessed from a Mobile Browser? - Tyche Softwares

Similar to the above one, you can also offer free gift on website visit irrespective of any device access. Check out our guide that well explains about how to automatically add a free product to your WooCommerce cart in 6 different conditions.

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