在 WooCommerce 中插入一个自定义总计,不包括购物车上的产品成本行和结帐总计 [英] Insert a custom total excluding products cost row on cart and checkout totals in WooCommerce
问题描述
我可以使用 在 Woocommerce 中的购物车和结帐总额中插入自定义总行 应答代码.(虽然我需要在底部添加我的)但我找不到或弄清楚如何计算我需要的总数.
I can add an extra row to the checkout totals table with Insert a custom total row on cart and checkout totals in Woocommerce answer code. (albeit I need to add mine at the bottom) but I can't find or figure out how to calculate the total that I need.
我需要添加的总数应包括除产品成本之外的所有内容(因此应包括运费、增值税、费用等).
The total I need to add should include everything apart from the product cost (so should include shipping, VAT, fees etc.).
我无法将产品价格更改为零,因为它们用于计算费用.
I can't change the product prices to zero as they're used for fee calculations.
我的代码尝试:
add_action( 'woocommerce_cart_totals_before_shipping', 'display_custom_total', 20 );
add_action( 'woocommerce_review_order_before_shipping', 'display_custom_total', 20 );
function display_custom_total() {
$total_to_pay = 0;
// Do something here
// The Output
echo ' <tr class="cart-total-to-pay">
<th>' . __( "Total to pay", "woocommerce" ) . '</th>
<td data-title="total-to-pay">' . number_format($total_to_pay, 2) . '</td>
</tr>';
}
我将如何将其添加到结帐中&购物车页面?
How would I go about adding this to the checkout & cart page?
推荐答案
要在底部显示,使用 woocommerce_cart_totals_after_order_total
&woocommerce_review_order_after_order_total
动作挂钩.
To display it at the bottom, use woocommerce_cart_totals_after_order_total
& woocommerce_review_order_after_order_total
action hook instead.
所以你得到:
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 );
相关:在 WooCommerce 订单电子邮件中插入不包括产品成本的自定义总额
这篇关于在 WooCommerce 中插入一个自定义总计,不包括购物车上的产品成本行和结帐总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!