首页 > 解决方案 > 在 WooCommerce 中以显示的格式小计获取税务信息

问题描述

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

在此处输入图像描述

除了购物车页面与其他人一起添加新字段外,我没有发现任何相关内容,所以实际上我想在小计字段中添加此税务信息。 显示小计,不包括。税,在 Woocommerce 结帐时添加小计税作为单独的行

标签: phpwordpresswoocommercecarttax

解决方案


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

// 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 文件中。测试和工作。


推荐阅读