Add An Additional Custom Checkbox In The WooCommerce Checkout | Code For Developers

Add an additional custom checkbox in the WooCommerce checkout

Add an additional custom checkbox after the terms and conditions in WooCommerce checkout we can use WooCommerce

To add an additional custom checkbox below the terms and conditions in WooCommerce checkout we can use WooCommerce “woocommerce_checkout_after_terms_and_conditions” hook. Here as an example, we are going to add the “personal data protection and privacy policy” checkbox after the terms and conditions.

It will be something like this..

Add an additional custom checkbox below the terms and conditions in WooCommerce checkout page

Now let’s write some code.

To add an additional custom checkbox below the terms and conditions in Wthe ooCommerce checkout page, Put this code on your functions.php in the child theme.

functions.php
// ===========================
// Custom checkbox
// ===========================
function privacy_checkbox_to_woocommerce_checkout() {
    ?>
    <p class="form-row validate-required">
        <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
            <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="privacy_checkbox">
            <span class="woocommerce-terms-and-conditions-checkbox-text">I have read and agree to <a 
 href="/privacy-policy/" class="woocommerce-privacy-link" target="_blank">personal data protection and privacy policy.</a></span> <span class="required">*</span>
        </label>
    </p>
<?php
}
add_action('woocommerce_checkout_after_terms_and_conditions', 'privacy_checkbox_to_woocommerce_checkout' );

Now your custom checkbox is visible on the WooCommerce Checkout page. We are not done yet. if you place an order now you can do without the checkbox checked !!!

So we need to put an error notice if the checkbox is not checked. something like this

Checkbox not checked Notice

Now put this code below of previous code to achieve this notice

funtions.php

// ====================================
// Notice if checkbox is not checked
// =====================================
function privacy_checkbox_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['privacy_checkbox'] ){
        wc_add_notice( __( 'Please read and accept the personal data protection and privacy policy to proceed with your order.' ), 'error' );
	}
}
add_action('woocommerce_checkout_process', 'privacy_checkbox_checkout_field_process');

That’s all. You are done. $_POST[‘privacy_checkbox’] privacy_checkbox is the name of our custom checkbox.

Thank you.

Previous Code

WooCommerce Ajax Product Search and Category Filter Without Plugin

WooCommerce Ajax Product Search with Category Filt ...

Next Code

Create a new WordPress administrator via functions.php & FTP

TweetShareSharePin0 Shares ...

Leave a Reply

Your email address will not be published. Required fields are marked *

If you find it useful

Buy me a coffee

ACF

Elementor

JavaScript

jQuery

Others

PHP

WooCommerce

WordPress

WP Plugin Dev

WooCommerce Discount based on Cart Item

A discount on the total order with condition based ...

WooCommerce Checkout Conflict with Bootstrap 4.x

WooCommerce Checkout Conflict with Bootstrap 4.x. ...

Update WooCommerce Delivery cost with condition

Change Delivery price base on what item in cart ...

WooCommerce Ajax Product Search and Category Filter Without Plugin

WooCommerce Ajax Product Search with Category Filt ...

Implementing WooCommerce Shop Page Quantity Input and Plus (+) Minus (-) Button with simple Code

Enhance the user experience on your WooCommerce we ...

top