WooCommerce客户邮件中调用自定义结账账户姓名字段解决方案问询
实现方案
直接替换你现有邮件模板里的对应代码即可,逻辑优先读取你存储的自定义账户姓名,取值失败时自动回退显示默认账单姓名,避免出现空白问候:
<?php // 读取订单中存储的自定义账户字段 $account_title = get_post_meta( $order->get_id(), ' Account Title', true ); $account_fname = get_post_meta( $order->get_id(), 'Account First Name', true ); $account_mname = get_post_meta( $order->get_id(), 'Account Middle Name', true ); $account_lname = get_post_meta( $order->get_id(), 'Account Last Name', true ); // 拼接完整自定义姓名,空字段会自动忽略 $custom_name = trim( implode( ' ', [ $account_title, $account_fname, $account_mname, $account_lname ] ) ); // 无自定义姓名时回退到默认账单姓名 $display_name = $custom_name ? $custom_name : $order->get_billing_first_name(); /* translators: %s: 客户姓名 */ ?> <p><?php printf( esc_html__( 'Hi %s,', 'woocommerce' ), esc_html( $display_name ) ); ?></p> <?php /* translators: %s: 订单编号 */ ?> <p><?php printf( esc_html__( 'Just to let you know — we\'ve received your order #%s, and it is now being processed:', 'woocommerce' ), esc_html( $order->get_order_number() ) ); ?></p>
注意说明
- 读取
Account Title字段时要注意你之前存储时meta key前面带了一个空格,必须和存储的key完全一致才能正常取值 - 你已经添加到functions.php的字段存储逻辑不需要改动,可正常使用
- 如果不需要显示称谓、中间名,直接删除
implode数组里的对应字段即可
内容的提问来源于stack exchange,提问作者Davy
相关产品推荐
相关产品推荐

