-
-
Save thegdshop/3171026 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* 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>'.__('My Checkbox: ').'</h3>'; | |
woocommerce_form_field( 'my_checkbox', array( | |
'type' => 'checkbox', | |
'class' => array('input-checkbox'), | |
'label' => __('I have read and agreed.'), | |
'required' => true, | |
), $checkout->get_value( 'my_checkbox' )); | |
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 my checkbox.') ); | |
} | |
/** | |
* 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'])); | |
} | |
?> |
Hi,
i try change location checkbox to woocommerce_checkout_after_terms_and_conditions.
When i change:
* Add checkbox field to the checkout **/ add_action('**woocommerce_checkout_after_terms_and_conditions**', 'my_custom_checkout_field');
i have error:
Fatal error: Call to a member function get_value() on string in
in line:
), $checkout->get_value( 'my_checkbox' ));
What is wrong?
@GamerDamian
woocommerce_checkout_after_terms_and_conditions
does not pass in the $checkout
variable, so you cant call a function on nil
see woocommerce hooks
If the checkbox isn't checked and throws an error, the form won't submit after it has been checked and the place order button is clicked again. It keeps throwing the error message. Any ideas?
How to add some amount in cart total if the checkbox is checked?
how can i add this checkbox after the payment methods ?
Hi there i am getting error on this add_action('woocommerce_checkout_fields', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter a number'),
), $checkout->get_value( 'my_field_name' ));
Fatal error: Call to a member function get_value() on array in C:\xampp\htdocs\flower\wp-content\themes\Avada-child\functions.php on line 286
What's wrong i am doing ?
Hello, I have tried this code snippit and found that if I leave a required field blank, I get an Internal Server Error. I am wondering what I can do to clear this up? Here is the code in my functions.php:
/** 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>'.__('Mail List: ').'</h3>';
woocommerce_form_field( 'my_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Please add my email address to your mail list to keep me up to date on future offerings.'),
'required' => false,
), $checkout->get_value( 'my_checkbox' ));
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( __('Would you like to join the mail list?') );
}
/**
- 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']));
}
For those of you getting the error with get_value on an array -- use WC_Checkout::get_value instead
Working fine for checkbox, Thanks for the resource.
Hello,
works perfectly, thank you very much!
However, could you help me adapt this code to put the checkout in two more languages? I am using WPML.
Thanks again.
I was getting an internal server error with the code above after my latest update. Fixed by changing $woocommerce->add_error to wc_add_notice (see comment by eduardo-marcolino above).