Saturday 18 June 2016

How to add new checkbox for terms & condition in checkout page for WordPress, WooCommerce Plugin?

Below given all code is single code, Put following code in theme "function.php" file:


/**
 * Add checkbox field to the checkout
 **/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');

function my_custom_checkout_field( $checkout ) {

    echo '<div id="my-new-field"><h3>'.__('Terms & Condition: ').'</h3>';
echo '<span class="mustcheck"><p>'.__('Must Check terms & condition *').'</p></span>';
    woocommerce_form_field( 'my_checkbox', array(
        'type'          => 'checkbox',
        'class'         => array('input-checkbox'),
        'label'         => __('Terms of Policy'),
        'required'  => true,
        ), $checkout->get_value( 'my_checkbox' ));

 
woocommerce_form_field( 'my_checkbox1', array(
        'type'          => 'checkbox',
        'class'         => array('input-checkbox'),
        'label'         => __('Terms of Use'),
        'required'  => true,
        ), $checkout->get_value( 'my_checkbox1' ));

 
woocommerce_form_field( 'my_checkbox2', array(
        'type'          => 'checkbox',
        'class'         => array('input-checkbox'),
        'label'         => __('Refund Policy'),
        'required'  => true,
        ), $checkout->get_value( 'my_checkbox2' ));

    echo '</div>';
}



/**
 * Process the checkout
 **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

    // Check if set, if its not set add an error.
    if (!$_POST['my_checkbox'])
         $woocommerce->add_error( __('Please agree to Terms of Policy.') );

if (!$_POST['my_checkbox1'])
         $woocommerce->add_error( __('Please agree to Terms of Use.') );

if (!$_POST['my_checkbox2'])
         $woocommerce->add_error( __('Please agree to Refund Policy.') );
}



/**
 * Update the order meta with field value
 **/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');

function my_custom_checkout_field_update_order_meta( $order_id ) {
    if ($_POST['my_checkbox']) update_post_meta( $order_id, 'My Checkbox', esc_attr($_POST['my_checkbox']));
if ($_POST['my_checkbox1']) update_post_meta( $order_id, 'My Checkbox1', esc_attr($_POST['my_checkbox1']));
if ($_POST['my_checkbox2']) update_post_meta( $order_id, 'My Checkbox2', esc_attr($_POST['my_checkbox2']));
}


Save it & Now Check Your checkout page.


For more Interesting, Useful Article & codes visit IT New Code.

Ankit Shah PHP Expert

IT New Code suggest a solution for your problem related to coding, Specially .PHP, Wordpress, WooCommerce, Magento, HTML and CSS. Visit Our website for Information.

1 comments:

  1. Hello! I was wondering if you would know how to run this script with a variable hide/show function. I need to have a a checkbox show up only for California residents and I can't seem to get it to work properly. Any help would be fantastic!

    ReplyDelete

 

Copyright @ 2016 IT New Code | Developing Code | Designing Code.

Designed by: Ankit Shah