在Woocommerce注册表单中添加一个条款和条件复选框 [英] Add a terms and conditions checkbox in Woocommerce registration form

查看:91
本文介绍了在Woocommerce注册表单中添加一个条款和条件复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce注册表格中,注册按钮前没有条款和条件".

In woocommerce registration form, there's no "terms and conditions" before sign up button.

有没有办法使它显示在表单中?

Is there a way to make it appears in the form?

这里是我的主题布局的链接.

推荐答案

已于2018年7月更新-增加了对Woocommerce 3.4+版本的兼容性

Updated on july 2018 - Added compatibility for Woocommerce version 3.4+

如果尚未完成,首先需要在结帐页面上启用条款:

If not done yet, first you need enable terms and conditions on checkout page:

  1. 要根据您的条款在Wordpress中创建新页面
  2. 要在Woocommerce>设置>结帐>结帐页面中启用该页面(部分):
  3. 然后保存...您完成了.
  1. To create a new page in Wordpress for your terms and conditions
  2. To enable that page in Woocommerce > Settings > Checkout > Checkout pages (section):
  3. Then save… you are done.


在注册表格上获得条款和条件复选框的代码:


The code to get the term and conditions check box on registration form:

// Add term and conditions check box on registration form
add_action( 'woocommerce_register_form', 'add_terms_and_conditions_to_registration', 20 );
function add_terms_and_conditions_to_registration() {

    if ( wc_get_page_id( 'terms' ) > 0 && is_account_page() ) {
        ?>
        <p class="form-row terms wc-terms-and-conditions">
            <label class="woocommerce-form__label woocommerce-form__label-for-checkbox checkbox">
                <input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="terms" <?php checked( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true ); ?> id="terms" /> <span><?php printf( __( 'I&rsquo;ve read and accept the <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-link">terms &amp; conditions</a>', 'woocommerce' ), esc_url( wc_get_page_permalink( 'terms' ) ) ); ?></span> <span class="required">*</span>
            </label>
            <input type="hidden" name="terms-field" value="1" />
        </p>
    <?php
    }
}

// Validate required term and conditions check box
add_action( 'woocommerce_register_post', 'terms_and_conditions_validation', 20, 3 );
function terms_and_conditions_validation( $username, $email, $validation_errors ) {
    if ( ! isset( $_POST['terms'] ) )
        $validation_errors->add( 'terms_error', __( 'Terms and condition are not checked!', 'woocommerce' ) );

    return $validation_errors;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.经过测试,可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

未选中复选框时,将显示错误消息.

When checkbox is not ticked, an error message is displayed.

此复选框仅是一个验证步骤(它不会保存在数据库中,因为我们不会在任何地方使用该信息).

This checkbox is only a validation step (It is not saved in database, as we don't use that information anywhere).


添加:

要仅在注册"页面上允许条款和条件,请使用以下其中一项:


Addition:

To allow terms and conditions only in Registration page use one of the following:

add_filter( 'woocommerce_checkout_show_terms', '__return_false' );

add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );

代码进入您的活动子主题(或活动主题)的function.php文件中.在WC 3.5+上进行了测试,并且可以正常工作.

Code goes in function.php file of your active child theme (or active theme). Tested on WC 3.5+ and works.

这篇关于在Woocommerce注册表单中添加一个条款和条件复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆