php - 在 WooCommerce 中插入自定义总计,不包括购物车上的产品成本行和结帐总计

标签 php wordpress woocommerce cart checkout

我可以使用 Insert a custom total row on cart and checkout totals in Woocommerce 在结帐总表中添加额外的一行答案代码。 (虽然我需要在底部添加我的)但我无法找到或弄清楚如何计算我需要的总数。

我需要添加的总额应包括产品成本之外的所有内容(因此应包括运费、增值税、费用等)。

我无法将产品价格更改为零,因为它们用于费用计算。

我的代码尝试:

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_totalwoocommerce_review_order_after_order_total 操作 Hook 。

所以你得到:

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 );

相关:Insert a custom total excluding products cost in WooCommerce order emails

关于php - 在 WooCommerce 中插入自定义总计,不包括购物车上的产品成本行和结帐总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67700661/

相关文章:

PHPExcel 将零输出为空白单元格

php - 在 woocommerce 中过滤货币并将其从 usd 转换为 inr for ccavenue

php - WooCommerce:使用价格覆盖将产品添加到购物车?

php - 获取订单项 woocommerce 订单

css - Woocommerce/WordPress : Featured Image/Product loading with background image

php - 根据 PHP url 变量显示特定模式

php - 单个网络服务器上的 PyPy + PHP

PHP Rest Api多个参数如何使用curl发送

javascript - 如何动态(或不)将javascript移动到页脚

html - Wordpress 博客循环,自动高度打破布局