php – Woocommerce中的欧洲GDPR额外结帐验证复选框

您好我一直在尝试在我的Woocommerce结帐页面添加一个额外的条件复选框,与条款和条件相同,但有关于新GDPR(数据保护)的信息和我的隐私政策的链接.他们必须在结账前打勾.

我一直在使用我在这里找到的各种代码片段,并设法添加一个条件复选框和一些文本,但无法添加隐私政策的链接.我必须在Checkout Box下面添加它.理想情况下,我希望在条款和条件之一的复选框旁边显示链接.

任何有助于在结帐框中获取隐私页面链接的帮助将不胜感激.

条件复选框隐私政策链接截图:

php  –  Woocommerce中的欧洲GDPR额外结帐验证复选框

这是我到目前为止所拥有的:

//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文件中.经过测试和工作.

php  –  Woocommerce中的欧洲GDPR额外结帐验证复选框

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

php  –  Woocommerce中的欧洲GDPR额外结帐验证复选框

上一篇:php – woocommerce在结帐和购物车页面更改价格


下一篇:php – Woocommerce中特定支付网关结账时的附加字段