You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP编写的WooCommerce客户通知邮件中插入换行符

解决方案

问题原因

  • 你使用的\n、\r\n是纯文本换行符,而WooCommerce订单通知是HTML格式邮件,这类换行符不会被渲染为可视换行,只会被识别为普通空格
  • 原有代码将英法两段文本封装在同一个<p>段落标签内,默认会连续展示不会分段

方案1:插入<br>标签实现换行(改动最小)

在两段文本中间插入HTML换行标签<br>,同时要在wp_kses的安全过滤规则中加入<br>标签的放行规则,避免标签被过滤删除,修改后代码如下:

<?php if ( $order->needs_payment() ) { ?>
 <p>
 <?php
 printf(
 wp_kses(
  /* translators: %1$s Site title, %2$s Order pay link */
  __( 'Voici le récapitulatif de votre commande effectuée sur %1$s. Veuillez procéder au règlement de la facture via ce lien sécurisé: %2$s <br> Here is the summary of your order made on %1$s. Please proceed to the payment of the invoice via this secure link: %2$s', 'woocommerce' ),
  array(
  'a' => array(
   'href' => array(),
  ),
  'br' => array()
  )
 ),
 esc_html( get_bloginfo( 'name', 'display' ) ),
 '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Payer pour cette commande - Pay for this order', 'woocommerce' ) . '</a>'
 );
 ?>
 </p>
<?php } ?>

方案2:拆分为两个独立段落(更符合需求效果)

直接将英法文本拆分为两个独立的<p>标签输出,和你期望的最终效果结构完全一致,修改后代码如下:

<?php if ( $order->needs_payment() ) { ?>
 <!-- 法语文本段 -->
 <p>
 <?php
 printf(
 wp_kses(
  /* translators: %1$s Site title, %2$s Order pay link */
  __( 'Voici le récapitulatif de votre commande effectuée sur %1$s. Veuillez procéder au règlement de la facture via ce lien sécurisé: %2$s', 'woocommerce' ),
  array(
  'a' => array(
   'href' => array(),
  ),
  )
 ),
 esc_html( get_bloginfo( 'name', 'display' ) ),
 '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Payer pour cette commande - Pay for this order', 'woocommerce' ) . '</a>'
 );
 ?>
 </p>
 <!-- 英语文本段 -->
 <p>
 <?php
 printf(
 wp_kses(
  /* translators: %1$s Site title, %2$s Order pay link */
  __( 'Here is the summary of your order made on %1$s. Please proceed to the payment of the invoice via this secure link: %2$s', 'woocommerce' ),
  array(
  'a' => array(
   'href' => array(),
  ),
  )
 ),
 esc_html( get_bloginfo( 'name', 'display' ) ),
 '<a href="' . esc_url( $order->get_checkout_payment_url() ) . '">' . esc_html__( 'Payer pour cette commande - Pay for this order', 'woocommerce' ) . '</a>'
 );
 ?>
 </p>
<?php } ?>

内容的提问来源于stack exchange,提问作者CM42

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 21:06:03