在WooCommerce注册上使AutomateWoo复选框为必需 编辑:

问题描述

AutomateWoo插件注册部分(当用户登录时位于“我的帐户”页面上)上添加一个复选框字段,我在这里尝试使其成为必填项。

我不希望人们在不勾选此框的情况下进行注册

该框是一个选择框,显示如下:

<p class="automatewoo-optin form-row">
    <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="automatewoo_optin" id="automatewoo_optin">
        <span class="automatewoo-optin__checkBox-text">I want to receive updates about products and promotions.</span>
    </label>
</p>

这是我的代码尝试使此复选框为“必需”:

add_filter( 'woocommerce_register_form_start','bd_require_automatewoo_optin');
function bd_require_automatewoo_optin( $fields ) {
$fields['automatewoo_optin']['required'] = true;
return $fields;
}

我也尝试使用woocommerce_register_formwoocommerce_forms_field钩子。

注册”表单的图像预览,复选框位于底部

Checkbox added to the bottom of the register form,must be made required.

如何在WooCommerce注册部分中使Make AutomateWoo复选框为必需?


编辑:

在某些情况下,您可能需要用“ true”替换第二个“ required”…

<script>
jQuery(document).ready(function($){
   $('input[name="automatewoo_optin"]').prop('required','required');
});
</script>

解决方法

要使位于WooCommerce注册表上的复选框字段必填,请使用以下内容:

select max(case when col0='Val1' then col0 end) col1,max(case when col0='Val2' then col0 end) col2,max(case when col0='Val3' then col0 end) col3,max(case when col0='Val4' then col0 end) col4
from tTable;

然后可选地还使用以下命令使复选框字段在视觉上是必需的:

// Validate WooCommerce registration form custom fields.
add_action( 'woocommerce_register_post','wc_validate_automatewoo_optin_fields',10,3 );
function wc_validate_automatewoo_optin_fields( $username,$email,$validation_errors ) {
    if ( ! isset($_POST['automatewoo_optin']) || empty($_POST['automatewoo_optin']) ) {
        $validation_errors->add('optin_error',__('The Checkbox is a required field','woocommerce') );
    }
        
    return $validation_errors;
}

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

,

使用jQuery。可以添加到主题脚本中,也可以添加到wp_footer并包装在<script> </script>

jQuery(document).ready(function($){
   $('input[name="automatewoo_optin"]').prop('checked',true);
});