Skip to content

Instantly share code, notes, and snippets.

@manlioma
Last active August 17, 2017 10:35
Show Gist options
  • Save manlioma/e20cd9295511a735afd84f404ba88e10 to your computer and use it in GitHub Desktop.
Save manlioma/e20cd9295511a735afd84f404ba88e10 to your computer and use it in GitHub Desktop.
WC - Add checkbox field to the checkout
/**
* Add checkbox field to the checkout: INFORMATIVA PRIVACY
**/
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
$home = home_url( '/' );
echo '<div id="privacy-checkbox"><h3>Informativa Privacy</h3>';
echo '<p>Leggi le <a href="'. esc_url( $home ) .'/informativa-privacy">condizioni di acquisto e l\'informativa sulla privacy</a></p>';
woocommerce_form_field( 'privacy_checkbox', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Accetto le condizioni'),
'required' => true,
), $checkout->get_value( 'my_checkbox' ));
echo '</div>';
}
/**
* Process the checkout INFORMATIVA PRIVACY
**/
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['privacy_checkbox'])
$woocommerce->add_error( __('Si prega di accettare le condizioni di acquisto.') );
}
/**
* 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['privacy_checkbox']) update_post_meta( $order_id, 'Accetto le condizioni', esc_attr($_POST['privacy_checkbox']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment