在Woocommerce中“客户运送”部分下的“管理员用户”中添加其他字段 [英] Add additional fields to Admin User under Customer shipping section in Woocommerce

查看:83
本文介绍了在Woocommerce中“客户运送”部分下的“管理员用户”中添加其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在woocommerce中,使用以下代码在自定义php文件中创建并注册了一些自定义帐户字段后:

 <标签为= reg_shipping_phone> ...< / label> 
< input class = ... id = reg_shipping_phone name = shipping_phone
type = tel pattern = [a-z0-9 .-] {6,40}
title = ... placeholder = ...
value =<?php esc_attr_e($ _POST ['shipping_phone']);?> />

并在我主题的function.php文件中:

  //在更改时保存管理员/&前端在更改中
add_action('personal_options_update','woo_save_extra_register_fields');
add_action(‘woocommerce_save_account_details’,‘woo_save_extra_register_fields’);

//从
add_action('woocommerce_created_customer','woo_save_extra_register_fields')中保存额外的注册字段数据;
函数woo_save_extra_register_fields($ customer_id)
{
if(isset($ _POST ['shipping_phone']))){
update_user_meta($ customer_id,'shipping_phone',$ _POST [' shipping_phone']);
}
}

我想实现此送货电话字段

如何在客户的收货地址部分中的后端用户个人资料页上的正确位置显示

表?



例如,我尝试了以下*(但 11,5 是错误的...)*:

  add_action('show_user_profile','extra_profile_fields',*** 11,5 ***) ; 
add_action('edit_user_profile','extra_profile_fields',*** 11,5 ***);

函数extra_profile_fields($ user){?>


< h3>表格名称< / h3>

< table class = form-table>

< tr>
< th>< label for = shipping_phone> SHIPPING PHONE< / label>< / th>

< td>
< input type = text name = shipping_phone id = shipping_phone value =<?php echo esc_attr($ user-> shipping_phone);?> class = regular-text />
< / td>
< / tr>
>

< / table>
<?php}

感谢您的帮助。

解决方案

如果要在用户的收货地址部分的后端用户个人资料中包含自定义的发运电话字段,请改用以下内容:

  add_filter('woocommerce_customer_meta_fields','filter_add_customer_meta_fields',10,1); 
函数filter_add_customer_meta_fields($ args){
$ args ['shipping'] ['fields'] ['shipping_phone'] = array(
'label'=> __('发运电话','woocommerce'),
'description'=>'',
);
返回$ args;
}

代码进入活动子主题(或活动主题)的function.php文件)。经过测试并有效。




此小代码段将完成所有操作。如果您在送货电话字段中更改值,则无需任何其他代码即可对其进行更新。



In woocommerce, after creating and registering some custom account fields in a custom php file using the following code:

<label for="reg_shipping_phone"> ... </label>
<input  class="..." id="reg_shipping_phone" name="shipping_phone" 
        type="tel" pattern="[a-z0-9.-]{6,40}"
        title=" ... " placeholder=" ... "
        value="<?php esc_attr_e( $_POST['shipping_phone'] ); ?>" />

and in my theme's function.php file:

//save on admin/&frontend on change
add_action( 'personal_options_update', 'woo_save_extra_register_fields' );    
add_action( 'woocommerce_save_account_details', 'woo_save_extra_register_fields' );

// Save extra registration fields data on from
add_action( 'woocommerce_created_customer', 'woo_save_extra_register_fields' );
function woo_save_extra_register_fields( $customer_id )
{
if( isset( $_POST['shipping_phone'] ) ) {
        update_user_meta( $customer_id, 'shipping_phone',  $_POST['shipping_phone'] );
    }
}

I would like to implement this "Shipping phone" field in the correct position on Backend User profile pages inside the "Customer's shipping address" section table

How can I insert additional fields in the "Customer's shipping address" section table?

For example I have tried the following *(but 11,5 is wrong...)*:

add_action( 'show_user_profile', 'extra_profile_fields',***11,5*** );
add_action( 'edit_user_profile', 'extra_profile_fields',***11,5*** );

function extra_profile_fields( $user ) { ?>


    <h3>NAME OF TABLE</h3>

    <table class="form-table">

        <tr>
            <th><label for="shipping_phone">SHIPPING PHONE</label></th>

            <td>
                <input type="text" name="shipping_phone" id="shipping_phone" value="<?php echo esc_attr(  $user->shipping_phone); ?>" class="regular-text" />
            </td>
        </tr>
>

    </table>
<?php }

Any help is appreciated.

解决方案

If you want to include your custom "Shipping phone" field in Backend User profile under "Customer's shipping address" section, you will use the following instead:

add_filter( 'woocommerce_customer_meta_fields', 'filter_add_customer_meta_fields', 10, 1 );
function filter_add_customer_meta_fields( $args ) {
    $args['shipping']['fields']['shipping_phone'] = array(
        'label'       => __( 'Shipping phone', 'woocommerce' ),
        'description' => '',
    );
    return $args;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

This little code snippet will do it all. If you change the value in this "Shipping phone" field, it will be updated without needing any additional code.

这篇关于在Woocommerce中“客户运送”部分下的“管理员用户”中添加其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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