【问题标题】:Customizing email subject with dynamic data in Woocommerce [closed]在 Woocommerce 中使用动态数据自定义电子邮件主题 [关闭]
【发布时间】:2018-07-06 16:54:20
【问题描述】:

不与 Display order total with and without VAT in Woocommerce's order email notification重复。

如何自定义电子邮件的主题(不是正文。我知道如何编辑正文中的变量,但相同的变量在其中不起作用出于某种原因的主题)。

所以我正在尝试更改 WooCommerce 订单电子邮件通知的主题

主题可通过 WooCommerce -> Settings -> E-mails -> New Order -> Manage... 自定义,根据 WooCommerce 文档,有 {order_date}{customer_name} 等变量...用于获取动态订单数据。

我的问题是我想在主题中显示“order number”和“order total”:

  • 如果我使用格式:Order {order_number} - {order_date} - {dollars_spent_order}
  • 我应该得到:Order # 123456 - July 6, 2018 - {dollars_spent_order}

但它不起作用。使用单花括号或双花括号没有区别。

如何将订单总额(包括税金、运费等)添加到电子邮件主题行?

我想得到以下结果:

订单号 123456 - 2018 年 7 月 6 日 - 1,234.56 美元

我用谷歌搜索了这个,但没有找到任何代码 sn-ps 告诉我如何做到这一点。

还有一个“WooCommerce Follow-Ups”扩展,增加了更多变量,但它是 99.00 美元,我不需要所有这些,只需要订单总额。某处必须有一个代码 sn-p...

【问题讨论】:

  • @Mohammad 抱歉,如果您注意先阅读标题和问题正文,您会发现此问题不能与您建议的链接答案重复 (其中之一我的答案).
  • 致社区:很抱歉,这个问题有一个明确的陈述,包括所需的行为、特定的问题或错误以及重现它所需的最短代码问题本身。所以请重新打开它,因为答案对社区有用。
  • 我也很想知道这个问题的答案。这不是重复的,电子邮件正文和主题行是不同的东西。

标签: php wordpress woocommerce orders email-notifications


【解决方案1】:

要自定义例如“新订单”电子邮件主题(发送给管理员),您将使用以下挂钩函数:

add_filter( 'woocommerce_email_subject_new_order', 'custom_email_subject', 20, 2 );
function custom_email_subject( $formated_subject, $order ){
    return sprintf( __('Order # %d - %s - %s', 'woocommerce'), 
        $order->get_id(), // Order ID
        $order->get_date_modified()->date_i18n('F j, Y'), // Formatted date modified
        wc_price($order->get_total()) // Order Total
    );
}

代码进入活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。


现在要自定义其他电子邮件通知(暂停、处理、已完成或发票),您可以使用以下任何过滤器挂钩:

  • woocommerce_email_subject_customer_on_hold_order
  • woocommerce_email_subject_customer_processing_order
  • woocommerce_email_subject_customer_completed_order
  • woocommerce_email_subject_customer_invoice
  • woocommerce_email_subject_customer_note
  • woocommerce_email_subject_low_stock
  • woocommerce_email_subject_no_stock
  • woocommerce_email_subject_backorder
  • woocommerce_email_subject_customer_new_account

加法:

如果你想去掉所有的html来保留没有html标签的格式化价格,你可以替换:

wc_price($order->get_total())

作者(感谢A K

html_entity_decode( strip_tags( wc_price( $order->get_total() ) ) );

【讨论】:

  • 95% 的路程。该变量确实出现了,但它被 HTML 包围:主题:订单号 123456 - 2018 年 7 月 11 日 - $ ;1.00 如何摆脱“”?
  • @TomJones999 您只需要保留 $order->get_total() 删除 woocommerce 格式化功能 wc_price(),但您需要在价格中添加货币符号,并且可能使用 number_format($order->get_total(), 2) 函数来保留 2小数...如果这个答案是回答你的问题,请don't forget to accept the answer,谢谢。
  • @TomJones999 您应该点击问题底部的“重新打开”链接,因为您的问题包含了所有需要的内容,而且不是题外话。
  • 我通过将一行更改为: html_entity_decode(strip_tags(wc_price($order->get_total()))) // 订单总数
  • @AK 是的,谢谢,我也一直在使用这个技巧……我在这个帖子的末尾添加了这个。
猜你喜欢
  • 2020-03-11
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 2018-01-20
相关资源
最近更新 更多