【问题标题】:Adding extra fields to the woocommerce form in wordpress在 wordpress 中向 woocommerce 表单添加额外字段
【发布时间】:2021-09-29 03:50:08
【问题描述】:

我已经设法在 woocommerce 注册表单中添加了几个额外的字段。但是在结帐表单中,我添加的所有字段都不会自动填充。结帐中未出现的字段是:“国家”和“州”。 正如您在此图像 (https://postimg.cc/vc57qkkF) 中看到的,对于国家和州字段,将出现一个选择。但我需要显示我注册时输入的国家和州。 这是我添加到functions.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'] : '' )
    );
    woocommerce_form_field(
        'billing_address_1',
        array(
            'type'        => 'text',
            'required'    => true, // just adds an "*"
            'label'       => 'Address'
        ),
        ( isset($_POST['billing_address_1']) ? $_POST['billing_address_1'] : '' )
    );
    woocommerce_form_field(
        'billing_city',
        array(
            'type'        => 'text',
            'required'    => true, // just adds an "*"
            'label'       => 'City'
        ),
        ( isset($_POST['billing_city']) ? $_POST['billing_city'] : '' )
    );
    woocommerce_form_field(
        'billing_postcode',
        array(
            'type'        => 'text',
            'required'    => true, // just adds an "*"
            'label'       => 'Post Code'
        ),
        ( isset($_POST['billing_postcode']) ? $_POST['billing_postcode'] : '' )
    );
    woocommerce_form_field(
        'billing_country',
        array(
            'type'        => 'text',
            'required'    => true, // just adds an "*"
            'label'       => 'Country'
        ),
        ( isset($_POST['billing_country']) ? $_POST['billing_country'] : '' )
    );
    woocommerce_form_field(
        'billing_state',
        array(
            'type'        => 'text',
            'required'    => true, // just adds an "*"
            'label'       => 'State'
        ),
        ( isset($_POST['billing_state']) ? $_POST['billing_state'] : '' )
    );
 
}
/**
 * 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!' );
    }
    if ( empty( $_POST['billing_address_1'] ) ) {
        $errors->add( 'billing_address_1_error', 'Address is required!' );
    }
    if ( empty( $_POST['billing_city'] ) ) {
        $errors->add( 'billing_city_error', 'City is required!' );
    }
    if ( empty( $_POST['billing_postcode'] ) ) {
        $errors->add( 'billing_postcode_error', 'Post code is required!' );
    }
    if ( empty( $_POST['billing_country'] ) ) {
        $errors->add( 'billing_country_error', 'Country is required!' );
    }
    if ( empty( $_POST['billing_state'] ) ) {
        $errors->add( 'billing_state_error', 'State 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'] ) );
    }
    // WooCommerce billing address
    if ( isset( $_POST['billing_address_1'] ) ) {
        update_user_meta( $customer_id, 'billing_address_1', sanitize_text_field( $_POST['billing_address_1'] ) );
    }
        // WooCommerce billing city
    if ( isset( $_POST['billing_city'] ) ) {
        update_user_meta( $customer_id, 'billing_city', sanitize_text_field( $_POST['billing_city'] ) );
    }
    if ( isset( $_POST['billing_postcode'] ) ) {
        update_user_meta( $customer_id, 'billing_postcode', sanitize_text_field( $_POST['billing_postcode'] ) );
    }
    if ( isset( $_POST['billing_country'] ) ) {
        update_user_meta( $customer_id, 'billing_country', sanitize_text_field( $_POST['billing_country'] ) );
    }
    if ( isset( $_POST['billing_state'] ) ) {
        update_user_meta( $customer_id, 'billing_state', sanitize_text_field( $_POST['billing_state'] ) );
    }
}
add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    我还不能添加 cmets,所以我在这里添加一个问题,并尽我所能同时回答。如果需要,将进行编辑。

    首先, 仅出于调试目的以确保我走在正确的轨道上,我猜这些字段也不会自动填充到“我的帐户”页面上,对吧?

    我看到你正在为国家和州做一个文本字段。您需要使用内置的 WooCommerce 的 Country 和 State 字段类型,以便将存储的值映射到选择选项,从而自动选择正确的选项。

    试试这个:

    1. 将此添加到您的 misha_add_register_form_field 函数顶部,以从 WC 获取国家/地区列表。
    $countries_obj = new WC_Countries();
    $countries = $countries_obj->__get('countries');
    
    1. 然后,要在表单中显示这些字段,请将 misha_add_register_form_field() 函数中的国家和州字段替换为:
    woocommerce_form_field(
        'billing_country',
        array(
          'type' => 'country',
          'label' => 'Country',
          'required' => 1,
          'class' => ['address-field']
        ),
        ''
      );
    
      woocommerce_form_field(
        'billing_state',
        array(
          'type' => 'state',
          'label' => 'State',
          'required' => 1,
          'class' => ['address-field'],
          'validate' => ['state']
        ),
        ''
      );
    

    您需要更新这 2 个字段的验证和清理程序,并且可能需要做一些额外的工作以确保正确更新。 您可以参考Using the WooCommerce API to get the country/state lists 和Add Billing Address to Woocommerce Registration Page,因为它们似乎解决了同样的挑战。

    【讨论】:

    • 是否可以通过 sn-p 而不是 functions.php 引入该代码?
    • sn-p 是什么意思?有几个选项可以说明这可能意味着什么,如果你也举个例子,请澄清一下,甚至更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    • 2010-11-12
    相关资源
    最近更新 更多