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.

Solution: Add BOGO Gifts When the Site is Accessed from a Mobile Browser

This code automatically adds a specific free product to the WooCommerce cart only for mobile users.

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');

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 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

To conclude, we have seen how to reward mobile shoppers and increase the likelihood of completing the purchase. Let’s explore more easy-to-follow strategies that will automatically add a free product to your WooCommerce cart in 6 different conditions. Such promotional tactics leave your customers delighted and ensure they are left with a happy and memorable shopping experience.

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