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

时间:2018-03-16 01:26:44

标签: php wordpress checkbox woocommerce registration

在woocommerce注册表中,注册按钮之前没有“条款和条件”。

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

这是the link of my theme layout

1 个答案:

答案 0 :(得分:8)

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

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

  1. 在Wordpress中为您的条款和条件创建新页面
  2. 在Woocommerce>中启用该页面设置>结帐>结帐页(部分)
    enter image description here
  3. 然后保存......你已经完成了。
  4. 在注册表单上获取条款和条件复选框的代码:

    // 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文件中。经过测试和工作。

    enter image description here

    如果未选中复选框,则会显示错误消息。

    enter image description here

      

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

    增加:

    要仅在注册页中允许使用条款和条件,请使用以下选项之一:

    add_filter( 'woocommerce_checkout_show_terms', '__return_false' );
    

    或者

    add_filter( 'woocommerce_get_terms_and_conditions_checkbox_text', '__return_false' );
    

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