在 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 注册表中添加条款和条件复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
    PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
    mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
    Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
    Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
    Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)