douliang2935 2018-10-04 16:32
浏览 93

在Woocommerce的“客户送货”部分下的管理员用户中添加其他字段

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.

  • 写回答

1条回答 默认 最新

  • doujingtang6580 2018-10-04 17:06
    关注

    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.

    enter image description here

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line