填充Woocommerce自定义字段 [英] Populating Woocommerce Custom Fields

查看:79
本文介绍了填充Woocommerce自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在填充Woocommerce注册页面上通过自定义字段获取的信息时遇到了问题,填写表格时,似乎唯一被拉到的东西是:名字,姓氏,城市,邮政编码,电话和电话;电子邮件地址.

I'm having issues populating information gained through custom fields on a Woocommerce sign up page, when filling out the form the only things that seem to get pulled over are: First Name, Last Name, City, Postcode, Phone & Email Address.

当用户注册我们的网站时,他们无法立即登录,因为他们的帐户需要得到管理员的批准,提取的信息将用于在激活帐户之前与该人联系.

When a user signs up for our site, they cannot immediately login as their account needs to be approved by an admin, the information pulling through will be used to contact the person prior to their account being activated.

我尝试使用var_dump($_POST)转储$_POST数组,但进行更改后,所有字段均无法正常工作.

I have tried dumping the $_POSTarray by using var_dump($_POST) but after making the change, none of the fields were working correctly.

请在下面查看我当前用于添加自定义字段的内容&填充帐户页面上的字段:

Please see below what I'm currently using to add custom fields & to populate fields on the account page:

这是我用来将自定义字段添加到我的woocommerce注册表单中的方法:

This is what I am using to add the custom fields to my woocommerce registration form:

function wooc_extra_register_fields() {?>
    <p class="form-row form-row-first">
    <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_first_name" id="reg_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="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_last_name" id="reg_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-wide">
    <label for="reg_billing_company"><?php _e( 'Company', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php esc_attr_e( $_POST['billing_company'] ); ?>" /></p>
    <p class="form-row form-row-wide">
    <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php esc_attr_e( $_POST['billing_phone'] ); ?>" /></p>
    <p class="form-row form-row-wide">
    <label for="reg_billing_adress_1"><?php _e( 'Adress Line 1', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_adress_1" id="reg_billing_adress_1" value="<?php esc_attr_e( $_POST['billing_address_1'] ); ?>" />
    </p>
    <p class="form-row form-row-wide">
    <label for="reg_billing_adress_2"><?php _e( 'Adress Line 2', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_adress_2" id="reg_billing_adress_2" value="<?php esc_attr_e( $_POST['billing_address_2'] ); ?>" />
    </p>
    <p class="form-row form-row-wide">
    <label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php esc_attr_e( $_POST['billing_city'] ); ?>" />
    </p>
    <p class="form-row form-row-wide">
    <label for="reg_billing_postcode"><?php _e( 'Postcode', 'woocommerce' ); ?><span class="required"> *</span></label>
    <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php esc_attr_e( $_POST['billing_postcode'] ); ?>" />
    </p>
    <div class="clear"></div>
    <?php
 }
 add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );

这是我用来在用户注册后填充后端用户"区域的内容:

This is what I'm using to populate the backend 'user' area once a user signs up:

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']));
    }
    //Phone 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']));
    }
    //Billing Adress 1 Field
    if (isset($_POST['billing_adress_1'])) {
        update_user_meta($customer_id, 'address_1', sanitize_text_field($_POST['billing_address_1']));
        update_user_meta($customer_id, 'billing_address_1', sanitize_text_field($_POST['billing_address_1']));
    }
    //Billing Adress 2 Field
    if (isset($_POST['billing_adress_2'])) {
            update_user_meta($customer_id, 'address_2', sanitize_text_field($_POST['billing_address_2']));
            update_user_meta($customer_id, 'billing_address_2', sanitize_text_field($_POST['billing_address_2']));
    }
    //Billing City Field
    if (isset($_POST['billing_city'])) {
            update_user_meta($customer_id, 'city', sanitize_text_field($_POST['billing_city']));
            update_user_meta($customer_id, 'billing_city', sanitize_text_field($_POST['billing_city']));
    }
    //Billing Postcode Field
    if (isset($_POST['billing_postcode'])) {
            update_user_meta($customer_id, 'postcode', sanitize_text_field($_POST['billing_postcode']));
            update_user_meta($customer_id, 'billing_postcode', sanitize_text_field($_POST['billing_postcode']));
    }
}
add_action('woocommerce_created_customer', 'text_domain_woo_save_reg_form_fields');

我不是PHP方面最有经验的人,因此任何帮助将不胜感激.

I'm not the most experienced in PHP, so any help would be massively appreciated.

推荐答案

第一个钩子函数中存在一些错误.另外,也不需要公司"和地址2"字段.

There is some mistakes in your first hooked function. Also "Company" and "Address 2" fields shouldn't be required.

在第二个挂钩函数中,公司"缺失,因此无论如何都不会保存.

In your 2nd hooked function, "Company" is missing, so it's not saved anyway.

要完成此操作,您需要为需要验证的字段添加一个附加的挂钩函数.

To finish You need an additional hooked function for fields required validation.

因此您的代码应为:

// Add extra registration fields
add_action( 'woocommerce_register_form_start', 'woo_add_extra_registration_fields', 20 );
function woo_add_extra_registration_fields() {?>
    <p class="form-row form-row-first">
        <label for="reg_billing_first_name"><?php _e( 'First name', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="billing_first_name" id="reg_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="reg_billing_last_name"><?php _e( 'Last name', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="billing_last_name" id="reg_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-wide">
        <label for="reg_billing_company"><?php _e( 'Company', 'woocommerce' ); ?></label>
        <input type="text" class="input-text" name="billing_company" id="reg_billing_company" value="<?php if ( ! empty( $_POST['billing_company'] ) ) esc_attr_e( $_POST['billing_company'] ); ?>" />
    </p>

    <p class="form-row form-row-wide">
        <label for="reg_billing_phone"><?php _e( 'Phone', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST['billing_phone'] ) ) esc_attr_e( $_POST['billing_phone'] ); ?>" />
    </p>

    <p class="form-row form-row-wide">
        <label for="reg_billing_address_1"><?php _e( 'Address Line 1', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="billing_address_1" id="reg_billing_address_1" value="<?php if ( ! empty( $_POST['billing_address_1'] ) ) esc_attr_e( $_POST['billing_address_1'] ); ?>" />
    </p>

    <p class="form-row form-row-wide">
        <label for="reg_billing_address_2"><?php _e( 'Adress Line 2', 'woocommerce' ); ?></label>
        <input type="text" class="input-text" name="billing_address_2" id="reg_billing_address_2" value="<?php if ( ! empty( $_POST['billing_address_2'] ) ) esc_attr_e( $_POST['billing_address_2'] ); ?>" />
    </p>

    <p class="form-row form-row-wide">
        <label for="reg_billing_city"><?php _e( 'City', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="billing_city" id="reg_billing_city" value="<?php if ( ! empty( $_POST['billing_city'] ) ) esc_attr_e( $_POST['billing_city'] ); ?>" />
    </p>

    <p class="form-row form-row-wide">
        <label for="reg_billing_postcode"><?php _e( 'Postcode', 'woocommerce' ); ?><span class="required"> *</span></label>
        <input type="text" class="input-text" name="billing_postcode" id="reg_billing_postcode" value="<?php if ( ! empty( $_POST['billing_postcode'] ) ) esc_attr_e( $_POST['billing_postcode'] ); ?>" />
    </p>

    <div class="clear"></div>
    <?php
}

// Validate require additional registration fields
add_action( 'woocommerce_register_post', 'woo_extra_registration_fields_validation', 20, 3 );
function woo_extra_registration_fields_validation( $username, $email, $validation_errors ) {
    $domain = 'woocommerce';
    $error  = '<strong>' . __( 'Error', $domain ) . '</strong>: ';

    // Billing First name field
    if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) )
        $validation_errors->add( 'billing_first_name_error', $error . __( 'First name is required!', $domain ) );

    // Billing Last name field
    if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) )
        $validation_errors->add( 'billing_last_name_error', $error . __( 'Last name is required!.', $domain ) );

    // Billing Phone field
    if ( isset($_POST['billing_phone']) && empty( $_POST['billing_phone'] ) )
        $validation_errors->add( 'billing_phone_error', $error . __( 'Phone is required!.', $domain ) );

    // Billing Adress 1 Field
    if ( isset($_POST['billing_address_1']) && empty( $_POST['billing_address_1'] ) )
        $validation_errors->add( 'billing_address_1_error', $error . __( 'Address is required!.', $domain ) );

    // Billing City Field
    if ( isset($_POST['billing_city']) && empty( $_POST['billing_city'] ) )
        $validation_errors->add( 'billing_city_error', $error . __( 'City is required!.', $domain ) );

    // Billing Postcode Field
    if ( isset($_POST['billing_phone']) && empty( $_POST['billing_phone'] ) )
        $validation_errors->add( 'billing_postcode_error', $error . __( 'Postcode is required!.', $domain ) );

    return $validation_errors;
}

// Save extra registration fields data
add_action('woocommerce_created_customer', 'woo_save_extra_registration_fields_data', 20, 1 );
function woo_save_extra_registration_fields_data( $customer_id ) {

    // Billing 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'] ) );
    }
    // Billing 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'] ) );
    }
    // Billing Company 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'] ) );
    }
    // Billing Phone 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'] ) );
    }
    // Billing Adress 1 Field
    if ( isset( $_POST['billing_address_1'] ) ) {
        update_user_meta( $customer_id, 'address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
        update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
    }
    // Billing Adress 2 Field
    if ( isset( $_POST['billing_address_2'] ) ) {
        update_user_meta( $customer_id, 'address_2', sanitize_text_field( $_POST['billing_address_2'] ) );
        update_user_meta( $customer_id, 'billing_address_2', sanitize_text_field( $_POST['billing_address_2'] ) );
    }
    // Billing City Field
    if ( isset( $_POST['billing_city'] ) ) {
        update_user_meta( $customer_id, 'city', sanitize_text_field( $_POST['billing_city'] ) );
        update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
    }
    // Billing Postcode Field
    if ( isset( $_POST['billing_postcode'] ) ) {
        update_user_meta( $customer_id, 'postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
        update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
    }
}

此代码将放在活动的子主题(或主题)的function.php文件上.经过测试并可以工作.

This code goes on function.php file of your active child theme (or theme). Tested and works.

这篇关于填充Woocommerce自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆