获取 WooCommerce 中显示的格式化小计中的税务信息

问题描述 投票:0回答:1

现在我正在显示所有价格的税费,例如总价和运费,现在我需要为小计添加相同的税值,如下图所示

enter image description here

我没有找到与此相关的任何内容,但对于购物车页面与其他页面添加新字段,所以实际上我想在小计字段中添加此税务信息。 显示小计,不含。税,在 Woocommerce 结帐时将小计税添加为单独的行

php wordpress woocommerce cart subtotal
1个回答
2
投票

要在购物车小计(订单和通知)中显示“71,540.20€(包括 11400,00€ 增值税)”,您可以尝试使用以下内容:

// Cart and checkout (for display including taxes - not compound)
add_filter('woocommerce_cart_subtotal', 'wc_cart_subtotal_incl_tax_amount_filter', 10, 3 );
function wc_cart_subtotal_incl_tax_amount_filter( $cart_subtotal, $compound, $cart ) {
    if ( wc_tax_enabled() && $cart->get_subtotal_tax() > 0 && ! $compound ) {
        $subtotal_tax   = wc_price( $cart->get_subtotal_tax() );
        $cart_subtotal  = wc_price( $cart->get_subtotal() + $cart->get_subtotal_tax() );
        $tax_label      = in_array( WC()->countries->get_base_country(), array_merge( WC()->countries->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
        $cart_subtotal .= sprintf( ' <small>' . esc_html__( '(includes %s %s)', 'woocommerce' ) . '</small>', $subtotal_tax, $tax_label );
    }
    return $cart_subtotal;
}

// Orders and emails (for display including taxes - not compound)
add_filter('woocommerce_order_subtotal_to_display', 'wc_order_subtotal_incl_tax_amount_filter', 10, 3 );
function wc_order_subtotal_incl_tax_amount_filter( $subtotal, $compound, $order ) {
    if ( wc_tax_enabled() && $order->get_total_tax() > 0 && ! $compound ) {
        $subtotal_tax = $subtotal = 0; // Initializing

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $subtotal     += $item->get_subtotal() + $item->get_subtotal_tax();
            $subtotal_tax += $item->get_subtotal_tax();
        }
        $subtotal     = wc_price( $subtotal, array( 'currency' => $order->get_currency() ) );
        $subtotal_tax = wc_price( $subtotal_tax, array( 'currency' => $order->get_currency() ) );
        $tax_label    = in_array( WC()->countries->get_base_country(), array_merge( WC()->countries->get_european_union_countries( 'eu_vat' ), array( 'NO' ) ), true ) ? __( 'VAT', 'woocommerce' ) : __( 'Tax', 'woocommerce' );
        $subtotal    .= sprintf( ' <small>' . esc_html__( '(includes %s %s)', 'woocommerce' ) . '</small>', $subtotal_tax, $tax_label );
    }
    return $subtotal;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.