如何将WordPress中WooCommerce分期支付邮件代码转为短信提醒
问题
现有一段可正常运行的WooCommerce分期支付邮件提醒PHP代码,想要复用逻辑实现短信提醒功能,但将邮件内容适配到短信发送代码后无法成功发送。相关代码如下:
邮件内容代码
// email body <?php if ( ! defined( 'ABSPATH' ) ) { exit ; // Exit if accessed directly } ?> <?php do_action( 'woocommerce_email_header', $email_heading, $email ) ; ?> <?php if ( $payment_order->has_status( 'pending' ) ) : ?> <p> <?php /* translators: 1: product title with installment number 2: payment number */ printf( wp_kses_post( __( 'Hi, <br>Your Invoice for %1$s from payment #%2$s has been generated. The Payment details are as follows', 'sumopaymentplans' ) ), wp_kses_post( $product_title_with_installment ), esc_html( $payment->get_payment_number() ) ) ; ?> </p> <?php endif ; ?> <?php do_action( 'woocommerce_email_before_order_table', $payment_order, $sent_to_admin, $plain_text, $email ) ; ?> <h2> <?php /* translators: 1: payment number */ printf( wp_kses_post( __( 'Payment #%s', 'sumopaymentplans' ) ), esc_html( $payment->get_payment_number() ) ) ; ?> </h2> <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1"> <thead> <tr> <th class="td" scope="col" style="text-align:left;"><?php esc_html_e( 'Product', 'sumopaymentplans' ) ; ?></th> <th class="td" scope="col" style="text-align:left;"><?php esc_html_e( 'Quantity', 'sumopaymentplans' ) ; ?></th> <th class="td" scope="col" style="text-align:left;"><?php esc_html_e( 'Price', 'sumopaymentplans' ) ; ?></th> </tr> </thead> <tbody> <?php echo wp_kses_post( wc_get_email_order_items( $payment_order, array( 'show_sku' => $sent_to_admin, 'show_image' => false, 'image_size' => array( 32, 32 ), 'plain_text' => $plain_text, 'sent_to_admin' => $sent_to_admin, ) ) ) ; ?> </tbody> <tfoot> <?php $text_align = is_rtl() ? 'right' : 'left' ; $item_totals = $payment_order->get_order_item_totals() ; if ( $item_totals ) { $i = 0 ; foreach ( $item_totals as $total ) { $i++ ; ?> <tr> <th class="td" scope="row" colspan="2" style="text-align:<?php echo esc_attr( $text_align ) ; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : '' ; ?>"><?php echo wp_kses_post( $total[ 'label' ] ) ; ?></th> <td class="td" style="text-align:<?php echo esc_attr( $text_align ) ; ?>; <?php echo ( 1 === $i ) ? 'border-top-width: 4px;' : '' ; ?>"><?php echo wp_kses_post( $total[ 'value' ] ) ; ?></td> </tr> <?php } } ?> </tfoot> </table> <p> <?php /* translators: 1: payment url 2: next payment date */ printf( wp_kses_post( __( 'Please make the payment using the payment link %1$s on or before <strong>%2$s</strong>', 'sumopaymentplans' ) ), '<a href="' . esc_url( $payment_order->get_checkout_payment_url() ) . '">' . esc_html__( 'pay', 'sumopaymentplans' ) . '</a>', esc_html( _sumo_pp_get_date_to_display( $payment->get_prop( 'next_payment_date' ), 'email' ) ) ) ; ?> </p> <?php do_action( 'woocommerce_email_footer', $email ) ; ?>
短信发送基础代码
// send sms <?php $to = $phone; $message = "body"; wp_sms_send( $to, $msg, true); ?>
已尝试的修改代码(无法运行)
// send sms <?php $to = $phone; $message = "<?php if ( $payment_order->has_status( 'pending' ) ) : ?> <?php /* translators: 1: product title with installment number 2: payment number */ printf( wp_kses_post( __( 'Hi, <br>Your Invoice for %1$s from payment #%2$s has been generated. The Payment details are as follows', 'sumopaymentplans' ) ), wp_kses_post( $product_title_with_installment ), esc_html( $payment->get_payment_number() ) ) ; ?> <?php endif ; ?> <?php /* translators: 1: payment number */ printf( wp_kses_post( __( 'Payment #%s', 'sumopaymentplans' ) ), esc_html( $payment->get_payment_number() ) ) ; ?> <?php /* translators: 1: payment url 2: next payment date */ printf( wp_kses_post( __( 'Please make the payment using the payment link %1$s on or before <strong>%2$s</strong>', 'sumopaymentplans' ) ), '<a href="' . esc_url( $payment_order->get_checkout_payment_url() ) . '">' . esc_html__( 'pay', 'sumopaymentplans' ) . '</a>', esc_html( _sumo_pp_get_date_to_display( $payment->get_prop( 'next_payment_date' ), 'email' ) ) ) ; ?>"; wp_sms_send( $to, $msg, true); ?>
解决方案
错误原因
- 语法错误:将PHP代码直接嵌入字符串中,PHP不会解析字符串内的
<?php ?>标签,导致$message内容为无效文本;同时存在变量名错误——wp_sms_send里用了$msg但定义的变量是$message。 - 格式不兼容:短信需要纯文本格式,保留HTML标签会导致内容混乱且可读性差。
正确实现代码
直接用PHP逻辑拼接纯文本短信内容,替代邮件的HTML渲染逻辑:
// send sms <?php if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly } $to = $phone; $message = ''; // 拼接订单状态提示 if ( $payment_order->has_status( 'pending' ) ) { $message .= sprintf( __( 'Hi, Your Invoice for %1$s from payment #%2$s has been generated. Payment details:', 'sumopaymentplans' ), $product_title_with_installment, esc_html( $payment->get_payment_number() ) ) . "\n\n"; } // 添加支付编号 $message .= sprintf( __( 'Payment #%s', 'sumopaymentplans' ), esc_html( $payment->get_payment_number() ) ) . "\n"; // 拼接商品明细 $message .= __( 'Products:', 'sumopaymentplans' ) . "\n"; foreach ( $payment_order->get_items() as $item ) { $message .= "- " . $item->get_name() . " x" . $item->get_quantity() . ": " . wc_price( $item->get_total() ) . "\n"; } // 拼接订单总价 $message .= "\n" . __( 'Totals:', 'sumopaymentplans' ) . "\n"; $item_totals = $payment_order->get_order_item_totals(); if ( $item_totals ) { foreach ( $item_totals as $total ) { $message .= $total['label'] . ": " . $total['value'] . "\n"; } } // 拼接支付链接和截止日期 $payment_link = esc_url( $payment_order->get_checkout_payment_url() ); $due_date = esc_html( _sumo_pp_get_date_to_display( $payment->get_prop( 'next_payment_date' ), 'email' ) ); $message .= "\n" . sprintf( __( 'Pay here: %1$s before %2$s', 'sumopaymentplans' ), $payment_link, $due_date ); // 发送短信(修正变量名错误) wp_sms_send( $to, $message, true); ?>
关键修改说明
- 移除所有HTML标签,用
\n实现换行,适配短信的纯文本阅读习惯 - 直接通过PHP逻辑拼接字符串,避免在字符串中嵌套PHP代码的语法错误
- 遍历订单商品生成简洁的纯文本明细,替代邮件专用的HTML渲染函数
wc_get_email_order_items - 修正变量名错误:将
wp_sms_send中的$msg改为定义好的$message - 简化翻译字符串,移除HTML相关占位符,适配纯文本格式
内容的提问来源于stack exchange,提问作者Godfrey Odhiambo
相关产品推荐
相关产品推荐

