1
votes

Right now i am displaying the tax for all hte prices like total and shipping, now i have the requirement to add the same tax values for the subtotals as well as shown in the following image

enter image description here

I have found nothing related to this but for the cart page to add new field with the others, SO actually i want to add this tax information with the subtotal field. Show subtotal excl. tax, add subtotal tax as separate row on Woocommerce checkout

1
no i mean currently in my case i am showing it including tax, then i want to append a line with the information that this subtotal includes 19 percent tax, as the other fields are showing in the picture above, so want to know a hook or override method ?shoaib Munir

1 Answers

1
votes

To display in cart subtotal (orders and notifications) like "71,540.20€ (includes 11400,00€ VAT)", you can try to use the following:

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

Code goes in functions.php file of your active child theme (or active theme). Tested and works.