Search code examples
wordpresswoocommerceaction-filterwordpress-hook

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


May I know how to display $user->billing_company upon results return when performing customer search in Sales Order form? Ideally am thinking of using filter/action hooks instead of modifying the core file in class-wc-meta-box-order-data.php

So far am able to search for customer based on the $user->billing_company, the results will show up on the editing order page, but not on the adding new order page ($user->billing_company is not displayed in real time upon typing and clicking on the searched result)

Images: https://i.sstatic.net/o4yfW.png

https://i.sstatic.net/pjfEM.png

Modified code:

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

Original code from 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 ); ?>

Thank you in advance!


Solution

  • The following hooked function will allow you to display the billing company without modifying core files:

    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;
    }
    

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

    enter image description here

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