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

2021-12-22 00:00:00 checkbox php wordpress woocommerce checkout

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

我一直在使用我在这里找到的各种代码片段,并设法添加了一个条件复选框和一些文本,但无法添加隐私政策的链接.我必须将它添加到结帐框下方.理想情况下,我希望将链接放在复选框旁边,就像条款和条件一上的那样.

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

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

这是我目前所拥有的:

//这里是添加复选框的函数:函数 cw_custom_checkbox_fields( $checkout ) {echo '<div class="cw_custom_class"><h3>'.__('给出分隔标题:').'</h3>';woocommerce_form_field( 'custom_checkbox', array('类型' =>'复选框','标签' =>__('协议政策.'),'必需' =>真的,), $checkout->get_value('custom_checkbox'));回声'</div>';}add_action('woocommerce_checkout_after_terms_and_conditions', 'checkout_additional_checkboxes');函数 checkout_additional_checkboxes( ){$checkbox1_text = __( "我已阅读并接受隐私政策并了解您如何根据 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></p><?php}add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');函数 my_custom_checkout_field_process() {//检查是否设置,如果未设置则添加错误.如果(!$_POST['custom_one'])wc_add_notice( __( '您必须接受我已阅读并接受隐私政策并了解您如何根据 GDPR 管理我的数据".' ), '错误' );}add_filter('comment_form_defaults','isa_comment_reform');add_action('woocommerce_after_checkout_form', 'wnd_checkout_message_bottom', 10);函数 wnd_checkout_message_bottom( ) {echo '<div class="wnd-checkout-message"><h3><a href="http://127.0.0.1/protest/privacy-policy/" target="_blank">隐私政策</a><br/></h3>

';}

解决方案

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

<块引用>

woocommerce 3.4 新版本处理现在 GDPR

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

//在结帐页面添加条款和政策复选框add_action('woocommerce_checkout_after_terms_and_conditions', 'add_terms_and_policy', 20);函数 add_terms_and_policy() {$domain = 'woocommerce';$gdpr_private_policy_link = sprintf('<a href="%s" target="_blank">%s</a>',home_url("/protest/privacy-policy/"),//GDPR隐私政策页面的按钮链接__( "隐私政策", $domain )//按钮文本);woocommerce_form_field( 'gdpr_terms', 数组('类型' =>'复选框','类' =>数组('条款gdpr_terms'),'input_class' =>数组('woocommerce-form__input-checkbox'),'label_class' =>数组('woocommerce-form__label-for-checkbox'),'标签' =>'<跨度>'.冲刺(__(我已阅读并接受 %s 并了解您如何根据 GDPR 管理我的数据", $domain ),$gdpr_private_policy_link).'</span>','必需' =>真的,), '');}//验证所需的 GDPR 私有策略复选框add_action('woocommerce_after_checkout_validation', 'terms_and_policy_validation', 20, 2);函数terms_and_policy_validation( $data, $errors ) {if ( !isset( $_POST['gdpr_terms'] ) ){$domain = 'woocommerce';$gdpr_text = sprintf(__(我已阅读并接受 %s 并了解您如何根据 GDPR 管理我的数据", $domain ),__( "隐私政策", $domain ));$errors->add('gdpr_terms', sprintf( __( 'You must accept "%s".', $domain ), $gdpr_text ), 'error' );}}

代码位于活动子主题(或活动主题)的 function.php 文件中.经过测试和工作.

如果不勾选,客户将收到此错误信息,避免结账:

Hi I have been trying to add an additional Conditional Check Box to my Woocommerce checkout page, the same as the Terms and Conditions one but with information about the new GDPR (data protection) and a link to my Privacy Policy. They must tick the box before they can check out.

I have been using various snippets of code put together from what I have found here and have managed to add a conditional check box and some text but have not been able to add a link to the Privacy Policy. I have to add it below the Checkout Box. Ideally I would like to have the link alongside the check box like it appears on the Terms and Conditions one.

Any help to get the Privacy page link within the checkout box would be appreciated.

Conditional Checkbox Privacy policy Link screenshot:

Here's what I have so far:

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

解决方案

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:

相关文章