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

    (@themehigh)

    Inorder to achieve your requirement, please add the below code in your child theme’s functions.php file.

    function disable_add_to_cart_till_required_fields_filled(){
    
    if(!is_product()){
    return;
    }
    
    $allowed = array(150, 152); // Update product id
    
    global $product;
    $id = $product->get_id();
    
    if(! in_array($id, $allowed)){
    return;
    }
    
    ?>
    
    <script type="text/javascript">
    (function($, window, document) {
    function may_disable_add_to_cart_button(){
    var req_fields = $('.thwepof-input-field.validate-required');
    var disabled = false;
    
    $.each(req_fields, function(index, elm){
    var value = '';
    var finput = $(elm);
    
    var type = finput.getType();
    var name = finput.prop('name');
    
    if(type == 'radio'){
    value = $("input[type=radio][name='"+name+"']:checked").val();
    
    }else if(type == 'checkbox'){
    value = $("input[type=checkbox][name='"+name+"']:checked").val();
    
    }else{
    value = finput.val();
    }
    
    if(isEmpty(value)){
    disabled = true;
    }
    });
    $('.single_add_to_cart_button').prop("disabled", disabled);
    }
    
    $.fn.getType = function(){
    try{
    return this[0].tagName == "INPUT" ? this[0].type.toLowerCase() : this[0].tagName.toLowerCase();
    }catch(err) {
    return 'E001';
    }
    }
    
    function isEmpty(str){
    return (!str || 0 === str.length);
    }
    
    $('.thwepof-input-field').on('keyup change', function(){
    may_disable_add_to_cart_button();
    });
    may_disable_add_to_cart_button();
    
    }(window.jQuery, window, document));
    </script>
    <?php
    }
    add_action('wp_footer', 'disable_add_to_cart_till_required_fields_filled');
    

    We hope this will help.

    Thank you!

    Thread Starter imaginemonkey

    (@imaginemonkey)

    Thank you!

    Thread Starter imaginemonkey

    (@imaginemonkey)

    Hey, I have one more question.

    This code worked wonderfully when we plugged in the product ID to the array.

    How can we also target a specific category to apply this code to all products in that specific category?

    Is there a new category array that you can add with an && operator alongside the product array?

    Plugin Author ThemeHigh

    (@themehigh)

    >> How can we also target a specific category to apply this code to all products in that specific category?

    For this, you need to replace the first section of the above code with the below one:

    function disable_add_to_cart_till_required_fields_filled(){
    
    if(!is_product()){
    return;
    }
    
    global $product;
    $product_id = $product->get_id();
    
    $allowed_category = array(19, 20, 21); // Update category ID
    $product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
    
    if(empty(array_intersect($allowed_category, $product_categories))){
    return;
    }
    
    ?>

    >> Is there a new category array that you can add with an && operator alongside the product array?

    You need to replace the first section of the code with the below one:

    function disable_add_to_cart_till_required_fields_filled(){
    
    if(!is_product()){
    return;
    }
    
    $allowed = array(150, 152); // Update product id
    
    global $product;
    $product_id = $product->get_id();
    
    if(! in_array($product_id, $allowed)){
    return;
    }
    
    $allowed_category = array(19, 20, 21); // Update category ID
    $product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids'));
    
    if(empty(array_intersect($allowed_category, $product_categories))){
    return;
    }
    
    ?>

    We hope this will help.

    Thank you!

    Thread Starter imaginemonkey

    (@imaginemonkey)

    Thank you!

    We purchased the PRO version of your plugin because you’ve been so awesome helping!

    If I have one more question about custom code, should I send you a message through your website’s support system?

    Plugin Author ThemeHigh

    (@themehigh)

    As per the WordPress forum policy, we can’t answer premium related questions on this forum. Please raise a ticket through our website. We hope our technical team will be able to help you.

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to disable “add to cart button” until checkbox is toggled?’ is closed to new replies.