Skip to content

Instantly share code, notes, and snippets.

@ddbs
Created March 14, 2014 00:27
Show Gist options
  • Save ddbs/9539955 to your computer and use it in GitHub Desktop.
Save ddbs/9539955 to your computer and use it in GitHub Desktop.
woocommerce: add terms & conditions
/**
* Add Terms and Conditions field to the checkout
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="terms_conditions"><h3>'.__('Terms and Conditions: ').'<a href="http://sylvanbotanical.org/fiveflavorsherbs/about/terms-and-conditions/">view here</a></h3>';
woocommerce_form_field( 'terms_conditions', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('I have read and agreed.'),
'required' => true,
), $checkout->get_value( 'terms_conditions' ));
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['terms_conditions'])
$woocommerce->add_error( __('Please agree to terms and conditions.') );
}
/**
* 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['terms_conditions']) update_post_meta( $order_id, 'Terms and Conditions', esc_attr($_POST['terms_conditions']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment