首页 > 解决方案 > 将自定义注册字段扩展到 WooCommerce 中的其他页面

问题描述

如何将自定义字段添加到 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('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;
}

但我无法在管理区域显示该字段。有什么帮助吗?

标签: phpwordpresswoocommercecustom-fields

解决方案


推荐阅读