php - 将自定义注册字段扩展到 WooCommerce 中的其他页面
问题描述
在如何将自定义字段添加到 WooCommerce 注册表单的帮助下,我能够添加自定义注册字段。
- 计费名字
- 计费姓氏
- 计费电话
- 计费 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', 'woocommerce'),
'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', 'woocommerce'), // Add custom field label
'placeholder' => _x('Cedula/RUC/Pasaporte', 'placeholder', 'woocommerce'), // 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;
}
但我无法在管理区域显示该字段。有什么帮助吗?
解决方案
推荐阅读
- azure - 数据源凭据存储在 Microsoft 集成运行时配置管理器中的什么位置?
- anylogic - 有没有一种方法可以随机生成从数据库创建的代理?
- c++ - 在 VS Code 中使用 C++ COM/IFileDialog 时出错,而在 Visual Studio 中使用相同的代码。消息:“IID_IFileOpenDialog”未在此范围内声明
- android - 如何使用 Kotlin 将 gRPC 添加到 Android Studio?
- amazon-web-services - 是否可以在不检查 ELB 配置的情况下找出 ELB 的所有 IP 地址?
- list - Numba 列表和 NamedTuple 签名
- python - 从匹配的数据帧拆分中查找下一个
- java - 本地主机连接在springboot中被拒绝
- reactjs - React 中的持久化状态
- django - 覆盖 Django 中所有模型的保存方法