在WooCommerce的购物车和结账总额中插入不包括产品成本行的自定义总额



我可以通过在购物车上插入自定义合计行并在Wooccommerce应答代码中添加结账合计,在结账合计表中添加一行。(尽管我需要在底部添加我的(,但我找不到或不知道如何计算我需要的总数。

我需要添加的总额应该包括所有,除了产品成本(因此应该包括运费、增值税、费用等(。

我不能把产品价格改为零,因为它们是用来计算费用的。

我的代码尝试:

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>';
}

我该如何将其添加到结账&购物车页面?

要在底部显示,请使用woocommerce_cart_totals_after_order_total&woocommerce_review_order_after_order_total操作挂钩。

所以你得到了:

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订单电子邮件中插入不包括产品成本的自定义总额

最新更新