如何让WooCommerce后台订单列表的账单与配送字段显示为大写?
解决WooCommerce后台订单字段显示大写的问题
你前台用CSS实现的只是显示层面的大写,数据本身还是用户输入的小写,而且后台的样式不受前台自定义CSS的影响,所以直接加相同CSS没用。下面给你两种可行的方案:
方案一:给后台添加自定义CSS(仅修改显示)
后台的字段选择器和前台不一样,你需要先确认后台订单页面里对应字段的ID或类名:
- 打开任意订单的编辑页面,右键点击账单名字输入框,选择「检查」,查看元素的ID(比如可能是
#_billing_first_name)。 - 在主题的
functions.php文件(或者使用代码片段插件)添加以下代码,把后台的目标字段改成大写显示:
// 给WooCommerce后台添加自定义CSS add_action('admin_enqueue_scripts', 'custom_woocommerce_admin_css'); function custom_woocommerce_admin_css() { // 只在订单编辑页面加载CSS global $pagenow; if ($pagenow === 'post.php' && get_post_type() === 'shop_order') { echo '<style type="text/css"> /* 替换成你实际找到的后台字段选择器 */ #_billing_first_name, #_billing_last_name, #_billing_city, #_billing_address_1 { text-transform: uppercase; } </style>'; } }
方案二:修改存储的数据(彻底解决,数据本身变为大写)
如果希望订单数据本身就存储为大写(不管前台后台都显示大写),可以在用户提交订单时就转换字段值,添加以下代码到functions.php:
// 结账时将指定账单字段转换为大写存储 add_action('woocommerce_checkout_update_order_meta', 'convert_billing_fields_to_uppercase'); function convert_billing_fields_to_uppercase($order_id) { // 处理名字 if (!empty($_POST['billing_first_name'])) { update_post_meta($order_id, '_billing_first_name', strtoupper($_POST['billing_first_name'])); } if (!empty($_POST['billing_last_name'])) { update_post_meta($order_id, '_billing_last_name', strtoupper($_POST['billing_last_name'])); } // 处理城市和地址 if (!empty($_POST['billing_city'])) { update_post_meta($order_id, '_billing_city', strtoupper($_POST['billing_city'])); } if (!empty($_POST['billing_address_1'])) { update_post_meta($order_id, '_billing_address_1', strtoupper($_POST['billing_address_1'])); } }
注意:修改functions.php前建议备份文件,或者使用「Code Snippets」插件来添加代码,避免出错导致网站崩溃。
内容的提问来源于stack exchange,提问作者Ramin Guliev
相关产品推荐
相关产品推荐

