Woocommerce - disable «place order» until user checks Privacy Policy

admin2025-06-06  7

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="">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?

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

Share Improve this question asked Nov 21, 2018 at 11:21 user3211760user3211760 111 silver badge2 bronze badges 1
  • Woocommerce has that feature inbuild? just make sure that page is set for privacy policy – user145078 Commented Nov 21, 2018 at 12:09
Add a comment  | 

1 Answer 1

Reset to default 3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749163539a316900.html

最新回复(0)