$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Woocommerce - disable «place order» until user checks Privacy Policy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far