php - WooCommerce 不会在发送给客户的电子邮件中显示自定义费用
问题描述
我已经构建了一个插件,它为来自 WooCommerce 的订单增加了费用。我使用带有 order_item_type 'fee' 的 wc_add_order_item 方法将此费用添加到订单中。我遇到的问题是该客户下订单时发送给客户的电子邮件中没有显示该费用。<tfoot>
如果我理解正确,WooCommerce 通常会使用 $order->get_order_item_totals() 从 email-order-details.php中添加所有费用和运费;并通过它们循环。
奇怪的是,当我试图寻找解决方案时,我遇到了“woocommerce_order_status_pending_to_processing_notification”钩子,这个钩子(如果我再次理解正确的话)在电子邮件发送给用户之前触发。在这个钩子的回调中,您将有一个订单 ID 供您使用,我在我的主题中的 functions.php 中调用了这个钩子。在回调中,我搜索了正确的订单并使用它来检查 $order->get_order_item_totals() 中的内容。我预计只有基本的东西,我的附加费用不会出现,但它确实出现了。
在将电子邮件发送给客户之前,我的费用在 $order->get_order_item_totals() 中可见,但 WooCommerce 不会在 email-order-details.php 中循环通过它,这怎么可能?还是我错过了什么?有什么想法吗?
最终目标是在发送给客户的电子邮件中包含我的定制费用。
作为参考,这是 email-order-details.php 中的循环:
$totals = $order->get_order_item_totals();
if ( $totals ) {
$i = 0;
foreach ( $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
}
}
解决方案
我不知道您使用哪个代码将费用添加到您的订单中,但我已在我的 woocommerce 订单中添加了服务费,它也显示在 woocommerce 电子邮件通知中。
尝试将此代码添加到您的functions.php 或使用它制作一个插件。您可以根据需要编辑费用金额。这是代码
/**
* Add a standard $ value service fee to all transactions in cart / checkout
*/
add_action( 'woocommerce_cart_calculate_fees','wc_add_svc_fee' );
function wc_add_svc_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// change the $fee to set the Service Fee to a value to suit
$fee = 1.00;
$woocommerce->cart->add_fee( 'Service Fee', $fee, true, 'standard' );
}
如果您希望费用或附加费占订单总额的百分比,请使用以下代码
/**
* Add a 1% surcharge to your cart / checkout
* change the $percentage to set the surcharge to a value to suit
*/
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.01;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Surcharge', $surcharge, true, '' );
}
使用上述任何代码,费用或附加费将出现在电子邮件通知中。
推荐阅读
- grep - 如何使用 grep for Windows 获取文本文件中第一个匹配项的第一部分?
- node.js - 在nodejs中以正确的方式读写typedArray
- python - asyncio 任务仅在调用 asyncio.sleep 后启动
- python - Python Logging 模块 - 记录器未登录正确的级别
- python - 具有滚动平均值的数据透视表
- sql - 如何创建一个 ISA 层次结构,其中 SQL 中只允许存在一种子类型?
- lua - 循环两个宏
- sql - 如何使用 curl 语句获取 influxDB 的列名(标签)?
- java - 使用递归函数列出给定字符串中所有可能的 IP 地址组合
- docker - Docker:来自守护进程的错误响应:未能在网桥上创建端点 priceless_noether: