Skip to content
Advertisement

Custom “Terms and Conditions” acceptance checkbox in Cart page Woocommerce

Works on small home project, and want to add checkmark option bellow “Proceed to Checkout” button into Cart page into my Woocommerce site. I found this function, that is add that option:

    add_action( 'woocommerce_after_cart', 'bbloomer_add_checkout_privacy_policy', 9 );

function bbloomer_add_checkout_privacy_policy() {

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'         => 'By proceeding, you are agreeing to our <a href="/termsandconditions">Terms and Conditions</a>',
)); 

}

// Show notice if customer does not tick

add_action( 'woocommerce_proceed_to_checkout', 'bbloomer_not_approved_privacy' );

function bbloomer_not_approved_privacy() {
    if ( ! (int) isset( $_POST['privacy_policy'] ) ) {
        wc_add_notice( __( 'Please acknowledge the Terms and Conditions' ), 'error' );
    }
}  

But seems that something is not fine with this function, because no matter if checked or not, customer can proceed to checkout page. I want to ensure that ONLY customers who will check the checkbox, can proceed to checkout. In opposite, warning message will be shown at top.

enter image description here

Can someone to tell me where is issue with this function or to point me to some working solution? Thanks in advance. This is image how currently looks with this function i posted before.

Advertisement

Answer

The following code will work in cart page for your privacy policy checkbox using mandatory jQuery code to make it work as “proceed to checkout” button is just linked to checkout page without submitting any data.

The code will disable the button on start:

enter image description here

It use “Sweet alert 2” JS message to display an error message on button click when the checkbox is not checked:

enter image description here

The complete code:

add_action( 'woocommerce_proceed_to_checkout', 'cart_privacy_policy_checkbox', 5 );
function cart_privacy_policy_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'         => sprintf( __( "I've read and accept the %s", "woocommerce" ),
                           '<a href="privacy-policy">'.__( "Privacy Policy", "woocommerce" ).'</a>' ),
    ));

    // jQuery code start below
    ?>
    <script src="https://unpkg.com/sweetalert2@8.8.1/dist/sweetalert2.all.min.js"></script>
    <script src="https://unpkg.com/promise-polyfill@8.1.0/dist/polyfill.min.js"></script>
    <script type="text/javascript">
    jQuery( function($){
        var a = '.checkout-button',     b = '#privacy_policy',
        c = '<?php echo wc_get_checkout_url(); ?>',     d = 'disabled';

        // Set disable button state
        $(a).addClass(d).prop('href', '#');

        // Woocommerce Ajax cart events
        $('body').on('updated_cart_totals removed_from_cart', function(){
            if( ! ( $(a).hasClass(d) && $(b).prop('checked') ) ){
                $(a).addClass(d).prop('href', '#');
            }
        })

        // On button click event
        $('body').on('click', a, function(e){
            if( ! $(b).prop('checked') ) {
                // Disable "Proceed to checkout" button
                e.preventDefault();
                // disabling button state
                if( ! $(a).hasClass(d) ){
                    $(a).addClass(d).prop('href', '#');
                }
                // Sweet alert 2
                swal({
                    title:  '<?php _e("Pricacy Policy", "woocommerce"); ?>',
                    text:   '<?php _e("Please acknowledge the privacy policy Terms and Conditions", "woocommerce"); ?>',
                    type:   'error',
                    timer:  3500,
                    showConfirmButton: false
                });
            }
        });

        // On checkbox change event
        $(b).change(function(){
            if($(this).prop('checked')){
                if( $(a).hasClass(d) ){
                    $(a).removeClass(d).prop('href', c);
                }
            } else {
                if( ! $(a).hasClass(d) ){
                    $(a).addClass(d).prop('href', '#');
                }
            }
        });
    });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Sweet Alert Two documentation

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement