如何使用hook将Woocommerce报表的customer列改为company name?

WooCommerce报表Customer列修改为Company Name方案
可以通过WooCommerce原生钩子实现修改,无需改动核心代码,操作如下:
- 修改列标题
使用manage_edit-shop_order_columns过滤器,将以下代码添加到站点主题的functions.php文件或自定义功能插件中即可生效:
add_filter( 'manage_edit-shop_order_columns', 'rename_customer_column_to_company', 20 ); function rename_customer_column_to_company( $columns ) { // 若你的后台列键为customer,可将下方billing_address替换为customer $columns['billing_address'] = 'Company Name'; return $columns; }
- (可选)修改列内容为账单公司名称
如果需要列内原本展示的客户姓名替换为下单时填写的公司名,可追加以下代码:
add_action( 'manage_shop_order_posts_custom_column', 'show_company_name_in_order_column', 10, 2 ); function show_company_name_in_order_column( $column, $order_id ) { if ( $column === 'billing_address' ) { $order = wc_get_order( $order_id ); $billing_company = $order->get_billing_company(); // 公司名为空时默认展示客户姓名作为兜底 echo !empty($billing_company) ? esc_html($billing_company) : $order->get_formatted_billing_full_name(); } }
适配说明:如果不确定当前后台customer列的键名,可临时在过滤器函数中打印$columns数组查看对应键值,替换代码中的对应参数即可适配。
内容的提问来源于stack exchange,提问作者Keith
相关产品推荐
相关产品推荐

