• Resolved mbadmin11

    (@mbadmin11)


    Hey there!
    I was wondering if thre is a way to limit the checkbox group to a certain numer of choices.
    For example I’d like my users to choose at least 3 options on the checkbox group list… is it possible?

    Thank you for your amazing work!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author ThemeHigh

    (@themehigh)

    Unfortunately, there is no direct option available in our plugin to achieve your requirement. However, you can achieve this using a custom javascript code.

    Initially, you have to add th-limit-checkbox in the Wrapper Class inside checkbox groups field. Then add the below code snippet in your child theme’s functions.php file.

    add_action('wp_footer', 'sa34er_limit_checkbox_group_selection', 9999);
    function sa34er_limit_checkbox_group_selection(){
    if(is_product()) {
    ?>
    <script>
    (function($, window, document) {
    $('.th-limit-checkbox input[type="checkbox"]').on('change', function() {
    if($('.th-limit-checkbox input[type="checkbox"]:checked').length > 3) { // Modify number
    this.checked = false;
    }
    });
    }(window.jQuery, window, document));
    </script>
    <?php
    }
    }

    We hope this will help.

    Thank you!

    Thread Starter mbadmin11

    (@mbadmin11)

    This is much appreciated! Your solution works perfectly as long as you select maximum 3 boxes, but you can still select just one or two.

    Is there a way to make it work as exact number? So the user must select exaclty trhee options, no more and no less?

    Thanks again!

    Plugin Author ThemeHigh

    (@themehigh)

    Please add the below code in your child theme’s functions.php file.

    add_action('wp_footer', 'fe36t_min_checkbox_group_selection', 9999);
    function fe36t_min_checkbox_group_selection(){
    	if(is_product()) {
    	?>
    		<script>
    			(function($, window, document) {
    				$(document).on('submit','form.cart',function(e){
    					if($('.th-limit-checkbox input[type="checkbox"]:checked').length < 3) { // Modify number
    						alert("Please select atleast 3 options");
    						e.preventDefault();
    					}
    				});
    			}(window.jQuery, window, document));
    		</script>
    	<?php
    	}
    }

    This will show a javascript warning if the customer clicks the add to cart button by selecting options less than 3.

    Please note that you have to keep the previous code snippet also in your child theme’s functions.php file.

    We hope this will help.

    Thank you!

    Thread Starter mbadmin11

    (@mbadmin11)

    I tried adding the code (while leaving the previous one) but nothing happens…am I doing something wrong?

    Thank you very much!

    Plugin Author ThemeHigh

    (@themehigh)

    Could you please raise a ticket through our website? We hope our technical team will be able to help you.

    Thank you!

    Plugin Author ThemeHigh

    (@themehigh)

    We believe that your issue is resolved.

    We are going to mark this thread as resolved.

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘checkbox group limit’ is closed to new replies.