在“添加新销售订单”页面执行客户搜索时显示 billing_company

Display billing_company when performing customer search in “Add New Sales Order” page

我可以知道如何在销售订单表单中执行客户搜索时在结果 return 上显示 $user->billing_company 吗?理想情况下,我正在考虑使用 filter/action 挂钩而不是修改 class-wc-meta-box-order-data.php

中的核心文件

目前可以根据$user->billing_company搜索客户,结果会出现在编辑订单页面,但不会出现在添加新订单页面($user->) billing_company 不是在输入和点击搜索结果时实时显示)

图片: https://i.stack.imgur.com/o4yfW.png

https://i.stack.imgur.com/pjfEM.png

修改后的代码:

esc_html__( '[%4$s] %1$s (#%2$s – %3$s)', 'woocommerce' )
        $user->display_name,
        absint( $user->ID ),
        $user->user_email, $user->billing_company

原始代码来自 class-wc-meta-box-order-data.php:

<?php
$user_string = '';
$user_id     = '';
if ( $order->get_user_id() ) {
    $user_id = absint( $order->get_user_id() );
    $user    = get_user_by( 'id', $user_id );
    /* translators: 1: user display name 2: user ID 3: user email */
    $user_string = sprintf(
        esc_html__( '%1$s (#%2$s &ndash; %3$s)', 'woocommerce' ),
        $user->display_name,
        absint( $user->ID ),
        $user->user_email
    );
}
?>
<select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
    <option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( $user_string ); ?></option>
</select>
<!--/email_off-->
</p>
<?php do_action( 'woocommerce_admin_order_data_after_order_details', $order ); ?>

提前致谢!

以下钩子函数将允许您在不修改核心文件的情况下显示账单公司:

add_filter(  'gettext',  'change_admin_single_order_heading3', 10, 3 );
add_filter(  'ngettext',  'change_admin_single_order_heading3', 10, 3 );
function change_admin_single_order_heading3( $translated, $text, $domain  ) {
    global $pagenow, $theorder;

    if ( is_admin() && $pagenow === 'post.php' && isset($_GET['post']) && get_post_type($_GET['post']) === 'shop_order' )
    {
        if( $text === '%1$s (#%2$s &ndash; %3$s)' && $domain === 'woocommerce' && $theorder->get_user_id() > 0 ){
            // Get user meta billing company
            if( $billing_company = get_user_meta( $theorder->get_user_id(), 'billing_company', true ) ) {
                $translated = esc_html__( '['.$billing_company.'] %1$s (#%2$s &ndash; %3$s)', $domain );
            }
        }
    }
    return $translated;
}

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

But you will not be able to display it when performing a search as the search choices are pulled with ajax…