问题描述
在 How to add custom fields to WooCommerce registration form 的帮助下,我能够添加自定义注册字段。
- 帐单名字
- 帐单姓氏
- 计费电话
- 计费 cedula(护照)
/**
* To add WooCommerce registration form custom fields.
*/
function text_domain_woo_reg_form_fields() {
?>
<p class="form-row form-row-first">
<label for="billing_first_name"><?PHP _e('Nombre','text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="billing_first_name" value="<?PHP if (!empty($_POST['billing_first_name'])) esc_attr_e($_POST['billing_first_name']); ?>" />
</p>
<p class="form-row form-row-last">
<label for="billing_last_name"><?PHP _e('Apellido','text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_last_name" id="billing_last_name" value="<?PHP if (!empty($_POST['billing_last_name'])) esc_attr_e($_POST['billing_last_name']); ?>" />
</p>
<p class="form-row form-row-first">
<label for="billing_phone"><?PHP _e('Teléfono','text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_phone" id="billing_phone" value="<?PHP if (!empty($_POST['billing_phone'])) esc_attr_e($_POST['billing_phone']); ?>" />
</p>
<p class="form-row form-row-last">
<label for="billing_cedula"><?PHP _e('Cédula/RUC/Pasaporte','text_domain'); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_cedula" id="billing_cedula" value="<?PHP if (!empty($_POST['billing_cedula'])) esc_attr_e($_POST['billing_cedula']); ?>" />
</p>
<div class="clear"></div>
<?PHP
}
add_action('woocommerce_register_form_start','text_domain_woo_reg_form_fields');
/**
* To validate WooCommerce registration form custom fields.
*/
function text_domain_woo_validate_reg_form_fields($username,$email,$validation_errors) {
if (isset($_POST['billing_first_name']) && empty($_POST['billing_first_name'])) {
$validation_errors->add('billing_first_name_error',__('<strong>Error</strong>: First name is required!','text_domain'));
}
if (isset($_POST['billing_last_name']) && empty($_POST['billing_last_name'])) {
$validation_errors->add('billing_last_name_error',__('<strong>Error</strong>: Last name is required!.','text_domain'));
}
if (isset($_POST['billing_phone']) && empty($_POST['billing_phone'])) {
$validation_errors->add('billing_phone_error',__('<strong>Error</strong>: Teléfono requerido!','text_domain'));
}
if (isset($_POST['billing_cedula']) && empty($_POST['billing_cedula'])) {
$validation_errors->add('billing_cedula',__('<strong>Error</strong>: Número de identificación requerido!','text_domain'));
}
return $validation_errors;
}
add_action('woocommerce_register_post','text_domain_woo_validate_reg_form_fields',10,3);
/**
* To save WooCommerce registration form custom fields.
*/
function text_domain_woo_save_reg_form_fields($customer_id) {
//First name field
if (isset($_POST['billing_first_name'])) {
update_user_Meta($customer_id,'first_name',sanitize_text_field($_POST['billing_first_name']));
update_user_Meta($customer_id,'billing_first_name',sanitize_text_field($_POST['billing_first_name']));
}
//Last name field
if (isset($_POST['billing_last_name'])) {
update_user_Meta($customer_id,'last_name',sanitize_text_field($_POST['billing_last_name']));
update_user_Meta($customer_id,'billing_last_name',sanitize_text_field($_POST['billing_last_name']));
}
//Telefono field
if (isset($_POST['billing_phone'])) {
update_user_Meta($customer_id,'phone',sanitize_text_field($_POST['billing_phone']));
update_user_Meta($customer_id,'billing_phone',sanitize_text_field($_POST['billing_phone']));
}
//Cedula field
if (isset($_POST['billing_cedula'])) {
update_user_Meta($customer_id,'cedula',sanitize_text_field($_POST['billing_cedula']));
update_user_Meta($customer_id,'billing_cedula',sanitize_text_field($_POST['billing_cedula']));
}
}
add_action('woocommerce_created_customer','text_domain_woo_save_reg_form_fields');
通过这两个片段,我可以在结帐页面和我帐户的地址页面中显示自定义字段 CEDULA。
// Hook in
add_filter( 'woocommerce_checkout_fields','custom_override_checkout_fields' );
// Our hooked in function – $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_cedula'] = array(
'label' => __('Cedula/RUC/Pasaporte','woocommerce'),'placeholder' => _x('Numero de identificacion','placeholder','required' => true,'class' => array('form-row-first'),'clear' => true
);
return $fields;
}
add_filter('woocommerce_billing_fields','custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing_cedula'] = array(
'label' => __('Cedula/RUC/Pasaporte',// Add custom field label
'placeholder' => _x('Cedula/RUC/Pasaporte',// Add custom field placeholder
'required' => true,// if field is required or not
'clear' => false,// add clear or not
'type' => 'text',// add field type
'class' => array('my-css') // add class name
);
return $fields;
}
但我无法在管理区域显示该字段。对此有什么帮助吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)