0

I've added a checkbox on the checkout page by using this :

add_action( 'woocommerce_review_order_before_submit', 'add_privacy_checkbox', 9 );
function add_privacy_checkbox() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-checkbox'),
'required' => true,
'label' => 'I\'ve read and accept the <a href="https://website.com/privacy-policy">Privacy Policy</a>',
));
}
add_action( 'woocommerce_checkout_process', 'privacy_checkbox_error_message' );
function privacy_checkbox_error_message() {
if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
wc_add_notice( __( 'You have to agree to our privacy policy in order to proceed' ), 'error' );
}
}

I want to make the checkout button disabled until the user has checked the "I've read and accept the privacy policy. What would be the best practice of doing this? With jQuery it would be the simplest way, but could it be done direct with php?

  • Woocommerce has that feature inbuild? just make sure that page is set for privacy policy – Latheesh V M Villa Nov 21 '18 at 12:09
1

I think you should jQuery to enable and disable the checkout button. Please try this script.

Put this script in the footer.php.

jQuery(window).on('load',function(){
        setTimeout(function(){
        jQuery('#payment #place_order').attr("disabled","disabled");
        console.log('Hello');
        },1000);            
    });
    jQuery(document).on('change','#privacy_policy_field #privacy_policy',function() {
     var ischecked= jQuery(this).is(':checked');
        if(!ischecked){
          jQuery('#payment #place_order').attr("disabled","disabled");
          console.log('unchecked');
        }else{
            jQuery('#payment #place_order').removeAttr("disabled");
            console.log('checked');
        }
    }); 

Note: This is tested script for your code.

  • Thank you! I've added this script. The console log works, but when the page loads, the checkout button is enabled. If i check the checkbox and then uncheck it, then it becomes disabled. – user3211760 Nov 21 '18 at 14:11
  • Do you mean the first time it's not working while loading page? – Gufran Hasan Nov 21 '18 at 14:14
  • Yes, the script loads but it doesn't make the button disabled by default. – user3211760 Nov 21 '18 at 14:24
  • Exactly another script also load before applying our script. Simply you can increase setTimeout. – Gufran Hasan Nov 21 '18 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.