在自定义WooCommerce插件中查询昵称而不是显示名称

时间:2017-04-05 作者:Harlan Bowling

我一直在为我的一个客户开发一个自定义插件,该插件将订单信息输出到Woocommerce的文本文件中。直到最近,一切都很顺利,因为某种无法解释的原因,每次有人登录时,他们的显示名都会从昵称更改为“FirstName LastName”选项。。。。这打断了我的输出。

我需要显示他们的昵称或登录名(相同),而不是显示名称。。。一直到现在都在使用。

以下是我认为是问题的相关部分:

            $query = $wpdb->prepare( "SELECT orden.ID AS order_number,
                orden.post_modified AS last_date,
                orden.post_excerpt AS deliverynotes,
                orden.post_modified AS formatteddate,
                user.ID AS user_id,
                user.user_email AS user_email,
                user.display_name AS user_name,
                user.user_login AS user_login
                FROM {$wpdb->posts} AS orden
                LEFT JOIN {$wpdb->postmeta} AS meta
                ON (meta.post_id = orden.ID)
                LEFT JOIN {$wpdb->users} AS user
                ON (user.ID=meta.meta_value )
                WHERE
                orden.post_type=\'shop_order\'
                AND orden.post_status not in (\'trash\',  \'auto-draft\')
                AND meta.meta_key=\'_customer_user\'
                AND orden.ID IN ({$orderIdsArrStr})" , \'\');

            $results = $wpdb->get_results($query);

            if ( !empty( $results ) ) {
                $OrderTXT = \'\';

                foreach($results as $result) {

                    $OrderId=$result->order_number;
                    $user_email=$result->user_email;
                    $user_id=$result->user_id;
                    $user_displayname = $result->user_name;
这是它为我显示订单的部分:

$OrderTXT .= "*,".$user_displayname.",".$OrderId.",".$shippingCustomer[\'client_firstname\']." ".$shippingCustomer[\'client_lastname\'].",".$shippingCustomer[\'$order_company\'].",".$shippingCustomer[\'client_firstname\'].",".$shippingCustomer[\'client_lastname\'].",".$shippingCustomer[\'$order_address1\'].",".$shippingCustomer[\'$order_address2\'].$shippingCustomer[\'$order_address3\'].",".$shippingCustomer[\'$order_city\'].",".$shippingCustomer[\'$order_state\'].",".$shippingCustomer[\'$order_postcode\'].",".$shippingCustomer[\'$order_country\'].",".$shippingCustomer[\'client_phone\'].",,".$user_email.",".$shippingCustomer[\'client_firstname\']." ".$shippingCustomer[\'client_lastname\'].",".$shippingCustomer[\'$order_company\'].",".$shippingCustomer[\'client_firstname\']." ".$shippingCustomer[\'client_lastname\'].",".$shippingCustomer[\'$order_address1\'].",".$shippingCustomer[\'$order_address2\'].$shippingCustomer[\'$order_address3\'].",".$shippingCustomer[\'$order_city\'].",".$shippingCustomer[\'$order_state\'].",".$shippingCustomer[\'$order_postcode\'].",".$shippingCustomer[\'$order_country\'].",".$shippingCustomer[\'client_phone\'].",UPS,00".$OrderId.",Internet,N45,\\n";
正是第一位“$user\\u displayname”改变并显示了错误的内容。如何将其更改为$user\\u昵称??

我已经尝试了$user\\u登录$user\\u昵称。。。。但无济于事。我猜是因为它不在原始查询中?

抱歉问了这么长时间。我相信这是一个非常简单的修复方法。

谢谢

1 个回复
SO网友:Andreas Knapp

我遇到了一个类似的问题,解决方法与此类似:

add_filter(\'pre_user_display_name\',\'default_display_name\');
function default_display_name($display_name) {
    if ( isset( $_POST[\'billing_first_name\'] ) ) {
        $display_name = sanitize_text_field( $_POST[\'billing_first_name\'] );
    }
    return $name;
}
我认为,新的woocommerce将每次结账时的disyplay\\u名称更改为First\\u name Last\\u name。使用“pre\\u user\\u display\\u name”,您可以更改自己的方式。

相关推荐

如何读取WordPress$Query关联数组(散列)键的值

WordPress编程新手(来自更为传统的环境),并试图了解其一些“独特”特性。我们的网站上有一个目录页,此代码驻留在functions.php, 如果条件为true,则调整结果。if( $query->is_post_type_archive( \'directory\' ) ){ ...//do stuff } 我想知道如何获取is_post_type_archive 这就是“目录”当我对值使用测试时。。。var_dumb($query->is_post