Update (May 2018 - Code enhanced)
The new version of woocommerce 3.4 handle now GDPR
The following code, will add an additional validation checkbox for the new mandatory european GDPR Privacy Policy, under terms and conditions, in checkout page:
// Add terms and policy check box in checkout page
add_action( 'woocommerce_checkout_after_terms_and_conditions', 'add_terms_and_policy', 20 );
function add_terms_and_policy() {
$domain = 'woocommerce';
$gdpr_private_policy_link = sprintf( '<a href="%s" target="_blank">%s</a>',
home_url("/protest/privacy-policy/"), // The button link to the GDPR privacy policy page
__( "Privacy Policy", $domain ) // The button text
);
woocommerce_form_field( 'gdpr_terms', array(
'type' => 'checkbox',
'class' => array( 'terms gdpr_terms' ),
'input_class' => array('woocommerce-form__input-checkbox'),
'label_class' => array('woocommerce-form__label-for-checkbox'),
'label' => '<span>' . sprintf(
__( "I have read and accept the %s and understand how you manage my Data under GDPR", $domain ),
$gdpr_private_policy_link
) . '</span>',
'required' => true,
), '');
}
// Validate required GDPR private policy checkbox
add_action( 'woocommerce_after_checkout_validation', 'terms_and_policy_validation', 20, 2 );
function terms_and_policy_validation( $data, $errors ) {
if ( ! isset( $_POST['gdpr_terms'] ) ){
$domain = 'woocommerce';
$gdpr_text = sprintf(
__( "I have read and accept the %s and understand how you manage my Data under GDPR", $domain ),
__( "Privacy Policy", $domain )
);
$errors->add( 'gdpr_terms', sprintf( __( 'You must accept "%s".', $domain ), $gdpr_text ), 'error' );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
If not checked, the customer will get this error message avoiding checkout:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…