沃梦达 / 编程问答 / php问题 / 正文

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

Add a terms and conditions checkbox in Woocommerce registration form(在 Woocommerce 注册表中添加条款和条件复选框)

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

问题描述

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

有没有办法让它出现在表单中?

这是

  • 然后保存……大功告成.

  • 获取注册表单上的条款和条件复选框的代码:

    //在注册表单上添加条款和条件复选框add_action('woocommerce_register_form', 'add_terms_and_conditions_to_registration', 20);函数 add_terms_and_conditions_to_registration() {如果 (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"><输入类型=复选框"class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox";名称=条款"<?php 已检查( apply_filters( 'woocommerce_terms_is_checked_default', isset( $_POST['terms'] ) ), true );?>id=条款"/><span><?php printf( __( '我已经阅读并接受了 <a href="%s" target="_blank" class="woocommerce-terms-and-conditions-链接>条款&amp;条件</a>', 'woocommerce'), esc_url(wc_get_page_permalink('terms')));?></span><span class="required">*</span><输入类型=隐藏"名称=术语字段"值=1"/></p><?php}}//验证所需的条款和条件复选框add_action('woocommerce_register_post', 'terms_and_conditions_validation', 20, 3);函数terms_and_conditions_validation( $username, $email, $validation_errors ) {if ( !isset( $_POST['terms'] ) )$validation_errors->add( 'terms_error', __( '条款和条件未检查!', 'woocommerce' ) );返回 $validation_errors;}

    代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

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

    <块引用>

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


    补充:

    仅在注册页面允许条款和条件,请使用以下方法之一:

    add_filter('woocommerce_checkout_show_terms', '__return_false');

    add_filter('woocommerce_get_terms_and_conditions_checkbox_text', '__return_false');

    代码位于活动子主题(或活动主题)的 function.php 文件中.已在 WC 3.5+ 上测试并有效.

    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?

    Here is the link of my theme layout.

    解决方案

    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. 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;
    }
    

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

    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). Tested on WC 3.5+ and works.

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

    本文标题为:在 Woocommerce 注册表中添加条款和条件复选框

    基础教程推荐