我见过类似的问题,但找不到适合我的解决方案。
我正在尝试将自定义字段添加到WooCommerce注册表单,特别是名字和姓氏字段。我已设法创建这些字段,但输入的信息不会在用户登录时传递到“帐户详细信息”页面。其他教程已提到验证字段,但我不确定它是否与我相关。我正在研究Wordpress儿童主题。
请访问codepad .org查看代码。 我尝试使用代码示例选项将代码粘贴到此处,但它无法正常工作。
希望我已经清楚地解释了自己。如果没有,请告诉我,我会澄清。
答案 0 :(得分:8)
我认为您已覆盖woocommerce/templates/myaccount/form-login.php
模板,通过这样做,您已设法显示billing_first_name
和billing_last_name
,但您忘记使用需要的woocommerce_created_customer
挂钩将这些数据保存到数据库中。
我建议您保持模板不变,然后通过function.php
以下是在WooCommerce注册表单中添加自定义字段的代码:
/**
* 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('First name', '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('Last name', '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>
<div class="clear"></div>
<?php
}
add_action('woocommerce_register_form_start', 'text_domain_woo_reg_form_fields');
关于验证问题的第二部分,它完全是可选的,取决于你的业务逻辑,你想要什么,一般来说,大多数网站都有名字和姓氏,但它完全取决于你,如果您不想验证此操作,请从上面的代码中删除<span class="required">*</span>
并跳过此部分。
/**
* 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'));
}
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']));
}
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');
以上所有代码都在您的活动子主题(或主题)的function.php
文件中。或者也可以在任何插件php文件中。
代码已经过测试并且功能齐全。
希望这有帮助!
答案 1 :(得分:2)
<?php
/**
* Add new register fields for WooCommerce registration
* To add WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_form', 'misha_add_register_form_field' );
function misha_add_register_form_field(){
woocommerce_form_field(
'billing_first_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'First name'
),
( isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : '' )
);
woocommerce_form_field(
'billing_last_name',
array(
'type' => 'text',
'required' => true, // just adds an "*"
'label' => 'Last name'
),
( isset($_POST['billing_last_name']) ? $_POST['billing_last_name'] : '' )
);
woocommerce_form_field(
'billing_phone',
array(
'type' => 'tel',
'required' => true, // just adds an "*"
'label' => 'Phone'
),
( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' )
);
}
/**
* To validate WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
function misha_validate_fields( $username, $email, $errors ) {
if ( empty( $_POST['billing_first_name'] ) ) {
$errors->add( 'billing_first_name_error', 'First name is required!' );
}
if ( empty( $_POST['billing_last_name'] ) ) {
$errors->add( 'billing_last_name_error', 'Last name is required!' );
}
if ( empty( $_POST['billing_phone'] ) ) {
$errors->add( 'billing_phone_error', 'Phone is required!' );
}
}
/**
* To save WooCommerce registration form custom fields.
*/
add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
function misha_save_register_fields( $customer_id ){
//First name field
if ( isset( $_POST['billing_first_name'] ) ) {
//update_user_meta( $customer_id, 'country_to_visit', wc_clean( $_POST['country_to_visit'] ) );
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']));
}
// WooCommerce billing phone
if ( isset( $_POST['billing_phone'] ) ) {
update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
}
}
?>
答案 2 :(得分:1)
使用过滤器钩子$arr = array (
0 =>
array (
'sku' => 'abc123',
'price' => '19.95',
'special_price' => '0',
'tier20' => '13.48',
'tier40' => '16.98',
'tier50' => '17.48',
),
1 =>
array (
'sku' => 'def456',
'price' => '129.98',
'special_price' => '79.98',
'tier50' => '123.48',
'tier100' => '116.98',
'tier250' => '110.48',
),
);
//Find all possible tiers
$tier =[];
foreach($arr as $sub){
$tier = array_merge($tier, preg_grep("/(tier.*)/", array_keys($sub)));
}
//Unique tiers
$tier = array_unique($tier);
//Count tiers
$columns = count($tier);
//Header of CSV with tiers
$header = 'sku, price, special_price, ' . implode(', ', $tier);
foreach($arr as $row => $sub){
foreach($sub as $key => $item){
// Add items to array (only first three are start)
$table[$row][$key] = $item;
// After first three items add empty placeholders with associative keys to tiers array
if(count($table[$row]) == 3){
$table[$row] = array_merge($table[$row], array_combine($tier, array_fill(3, $columns, 0)));
}
}
}
//For debug purpose, output the CSV header then each row.
echo $header . "\n";
foreach($table as $row){
echo implode(', ', $row) . "\n";
}
//var_dump($tier, $header, $table);
添加Woocommerce注册表单自定义字段,并通过随机数验证保存数据。
woocommerce_forms_field