简体   繁体   English

在Woocommerce注册表单中添加条款和条件复选框

[英]Add a terms and conditions checkbox in Woocommerce registration form

In woocommerce registration form, there's no "terms and conditions" before sign up button. 在woocommerce注册表中,注册按钮之前没有“条款和条件”。

Is there a way to make it appears in the form? 有没有办法让它出现在表格中?

Here is the link of my theme layout . 这是我的主题布局的链接

Updated on july 2018 - Added compatibility for Woocommerce version 3.4+ 更新于2018年7月 - 增加了Woocommerce版本3.4+的兼容性

If not done yet, first you need enable terms and conditions on checkout page: 如果尚未完成,首先您需要在结帐页面上启用条款和条件:

  1. To create a new page in Wordpress for your terms and conditions 在Wordpress中为您的条款和条件创建新页面
  2. To enable that page in Woocommerce > Settings > Checkout > Checkout pages (section) : 要在Woocommerce>设置>结帐>结帐页面(部分)中启用该页面:
    在此输入图像描述
  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;
}

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 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' );

Or 要么

add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested on WC 3.5+ and works. 在WC 3.5+上测试并且有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将复选框添加到Wordpress登录表单-条款和条件 - Add Checkbox to Wordpress Login Form - Terms and Conditions 如何验证woocommerce注册表单的同意条款复选框? - How do I validate agree terms checkbox for woocommerce registration form? 添加复选框以注册主题我的登录表格 - 条款和条件 - Add Checkbox to register Form of theme my login - Terms and Conditions 将条款和条件复选框移动到购物车页面-Woocommerce - Moving terms & conditions checkbox to cart page - Woocommerce 在Woocommerce注册和结帐页面中添加自定义复选框 - Add a custom checkbox in Woocommerce registration and checkout pages WooCommerce 中特定产品类别的自定义条款和条件复选框 - Custom terms and conditions checkbox for a specific product category in WooCommerce 如何在 Woocommerce 结帐中编辑条款和条件复选框文本? - How to edit terms & conditions checkbox text in Woocommerce checkout? 购物车页面 Woocommerce 中的自定义“条款和条件”接受复选框 - Custom "Terms and Conditions" acceptance checkbox in Cart page Woocommerce 使用PHP注册表格中的单选按钮同意或不同意条款和条件 - Using radio buttons in PHP registration form to agree or disagree to Terms and Conditions 在注册表单的WordPress(WooCommerce)中添加OTP验证 - Add OTP verification in WordPress(WooCommerce) in Registration Form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM