在 WooCommerce 订单电子邮件中插入不包括产品成本的自定义总计 [英] Insert a custom total excluding products cost in WooCommerce order emails
问题描述
我正在使用以下函数向购物车和结帐页面添加自定义总计(取自 this answer我的问题 插入自定义总计,不包括 WooCommerce 中购物车上的产品成本行和结帐总计)
I'm using the below function to add a custom total to the cart and checkout pages (taken from this answer to my question Insert a custom total excluding products cost row on cart and checkout totals in WooCommerce)
function display_custom_total() {
// Get (sub)total
$subtotal = WC()->cart->subtotal;
$total = WC()->cart->total;
// Calculate
$total_to_pay = $total - $subtotal;
// The Output
echo ' <tr class="cart-total-to-pay">
<th>' . __( 'Total (to pay)', 'woocommerce' ) . '</th>
<td data-title="total-to-pay">' . wc_price( $total_to_pay ) . '</td>
</tr>';
}
add_action( 'woocommerce_cart_totals_after_order_total', 'display_custom_total', 20 );
add_action( 'woocommerce_review_order_after_order_total', 'display_custom_total', 20 );
如何将其添加到电子邮件中的订单总计表中?
How can I add this to the order totals table in the emails as well?
我可以使用与钩子 woocommerce_email_after_order_table
相同的函数将其添加到电子邮件模板中,但将其添加到订单表下方而不是作为额外的行.
I can add it to the email templates using the same function with the hook woocommerce_email_after_order_table
but that adds it below the order table rather than as an extra row.
我知道我可以在我的子主题(特别是 email-order-details.php
)中编辑电子邮件模板文件,但我不知道如何编辑它以将计算添加到此行下方的新表格行:
I know that I can edit the email template files in my child theme (particularly email-order-details.php
) but I'm not sure how to edit it to add the calculation into a new table row below this one:
<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>
我还需要将其添加到我的帐户"中的页面 >查看订单(如果它可以是同一功能的一部分).
I also need to add it to the page in My Account > View Order (if it can be part of the same function).
推荐答案
您可以使用 woocommerce_get_order_item_totals
过滤器钩子,这将允许您向现有表添加新行.
You can use the woocommerce_get_order_item_totals
filter hook, which will allow you to add a new row to the existing tables.
新行将添加到:
- 电子邮件
- 已收到订单(感谢页面)
- 我的帐户 ->查看订单
function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Get (sub)total
$subtotal = $order->get_subtotal();
$total = $order->get_total();
// Calculate
$total_to_pay = $total - $subtotal;
// Add new row
$total_rows['total_to_pay']['label'] = __( 'Total to pay', 'woocommerce' );
$total_rows['total_to_pay']['value'] = wc_price( $total_to_pay );
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );
相关:在 WooCommerce 中插入不包括购物车上的产品成本行和结帐总额的自定义总额
这篇关于在 WooCommerce 订单电子邮件中插入不包括产品成本的自定义总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!