新的账单字段未显示在用户页面上(WooCommerce的穷人瑞士刀插件)

新的账单字段未显示在用户页面上(WooCommerce的穷人瑞士刀插件),woocommerce,Woocommerce,使用WooCommerce的穷人瑞士刀插件,我创建了3个新的账单字段:生日日期、时事通讯选择:是/否和条款与条件选择:是/否。我在网站上成功注册为新客户并填写了这些新字段 然而,当检查在仪表板中创建的帐户时,我看到了所有常规字段,除了我用穷人瑞士刀插件创建的3个字段。为什么会这样?很明显,插件并没有将字段数据发送到用户帐户 使用functions.php文件中的自定义字段可以获得相同的结果 // Hook in add_filter( 'woocommerce_checkout_fields'

使用WooCommerce的穷人瑞士刀插件,我创建了3个新的账单字段:生日日期、时事通讯选择:是/否和条款与条件选择:是/否。我在网站上成功注册为新客户并填写了这些新字段


然而,当检查在仪表板中创建的帐户时,我看到了所有常规字段,除了我用穷人瑞士刀插件创建的3个字段。为什么会这样?

很明显,插件并没有将字段数据发送到用户帐户

使用functions.php文件中的自定义字段可以获得相同的结果

// 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['shipping']['shipping_phone'] = array(
        'label'     => __('Phone', 'woocommerce'),
    'placeholder'   => _x('Phone', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}
要使其成为必需,请使用此代码:

/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an error.
    if ( ! $_POST['my_field_name'] )
        wc_add_notice( __( 'Please enter something into this new shiny field.' ), 'error' );
}
您可以在此网站上找到更多信息:


我认为账单字段仅在订单上可见,而不是用户。用户字段可以通过注册表单和用户注册挂钩获得。

这是我提出的一个解决方案,对我来说效果很好,希望它也能帮助你

我使用该插件为账单表单创建了几个新字段,但我们仅以其中一个为例:

-转售许可证账单\u转售\u许可证\u

现在,插件或第一个答案中的代码将为您将字段添加到表单中,在我的情况下,我最终得到:

<input type="text" class="input-text " name="billing_resale_license_" id="billing_resale_license_" placeholder="" value="" display="text">
既然数据存储在user_meta中,我们需要连接到概要文件区域以显示它,并允许用户或管理员编辑它

add_action( 'show_user_profile', 'ws_update_user_profile' );
add_action( 'edit_user_profile', 'ws_update_user_profile' );

function ws_update_user_profile( $user ){ ?>
<h3>Additional Fields</h3>

<table class="form-table">
    <tr>
        <th><label for="billing_resale_license_">Resale #</label></th>
        <td><input type="text" name="billing_resale_license_" value="<?php echo esc_attr(get_the_author_meta( 'billing_resale_license_', $user->ID )); ?>" class="regular-text" /></td>
    </tr>
</table>

我选择将我的其他字段拆分到默认Woocommerce字段上方的表中,因为我有很多字段,而且它们更具体地针对用户,而不是订单处理本身,所以我将它们组织在自己的名为“其他字段”的表中。

谢谢,但尽管该字段确实在签出过程中出现,它仍然没有显示在仪表板用户页面上。
add_action( 'show_user_profile', 'ws_update_user_profile' );
add_action( 'edit_user_profile', 'ws_update_user_profile' );

function ws_update_user_profile( $user ){ ?>
<h3>Additional Fields</h3>

<table class="form-table">
    <tr>
        <th><label for="billing_resale_license_">Resale #</label></th>
        <td><input type="text" name="billing_resale_license_" value="<?php echo esc_attr(get_the_author_meta( 'billing_resale_license_', $user->ID )); ?>" class="regular-text" /></td>
    </tr>
</table>
add_action( 'personal_options_update', 'save_extra_fields' );
add_action( 'edit_user_profile_update', 'save_extra_fields' );

function save_extra_fields( $user_id ){
    update_user_meta( $user_id,'billing_resale_license_', sanitize_text_field( $_POST['billing_resale_license_'] ) );
}