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.
/**
* 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']));
}
For more Interesting, Useful Article & codes visit IT New Code.
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