Woocommerce 中的欧洲 GDPR 附加结帐验证复选框

European GDPR additional checkout validation checkbox in Woocommerce

您好,我一直在尝试向我的 Woocommerce 结帐页面添加一个额外的条件复选框,与条款和条件相同,但包含有关新 GDPR(数据保护)的信息和 link我的隐私政策。他们必须在方框内打勾才能退房。

我一直在使用我在这里找到的各种代码片段,并设法添加了一个条件复选框和一些文本,但未能在隐私政策中添加 link .我必须将它添加到结帐框下方。理想情况下,我希望 link 与复选框一起显示在条款和条件中。

如能帮助我们在结帐框中获取隐私页面 link,我们将不胜感激。

有条件的复选框隐私政策Link 截图:

这是我目前的情况:

//Here is the function for adding the checkbox:
function cw_custom_checkbox_fields( $checkout ) {

    echo '<div class="cw_custom_class"><h3>'.__('Give Sepration Heading: ').'</h3>';

    woocommerce_form_field( 'custom_checkbox', array(
        'type'          => 'checkbox',
        'label'         => __('Agreegation Policy.'),
        'required'  => true,
    ), $checkout->get_value( 'custom_checkbox' ));

    echo '</div>';
}
add_action('woocommerce_checkout_after_terms_and_conditions', 'checkout_additional_checkboxes');
function checkout_additional_checkboxes( ){
    $checkbox1_text = __( "I have read and accept the Privacy Policy and understand how you manage my Data under GDPR", "woocommerce" );
       ?>
    <p class="form-row custom-checkboxes">
        <label class="woocommerce-form__label checkbox custom-one">
            <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_one" > <span><?php echo  $checkbox1_text; ?></span> <span class="required">*</span>
        </label>
            </p>
    <?php
}

add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['custom_one'] )
        wc_add_notice( __( 'You must accept "I have read and accept the Privacy Policy and understand how you manage my Data under GDPR".' ), 'error' );
  }




add_filter('comment_form_defaults','isa_comment_reform');
add_action( 'woocommerce_after_checkout_form', 'wnd_checkout_message_bottom', 10 );

function wnd_checkout_message_bottom( ) {
 echo '<div class="wnd-checkout-message"><h3> <a href="http://127.0.0.1/protest/privacy-policy/" target="_blank">Privacy Policy</a><br/></h3>
</div>';
}

更新 (2018 年 5 月 - 代码增强)

The new version of woocommerce 3.4 handle now GDPR

以下代码将根据条款和条件在结帐页面中为新的强制性欧洲 GDPR 隐私政策添加额外的验证复选框:

// 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' );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。测试和工作。

如果未选中,客户将收到此避免结账的错误消息: