Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

503
Vistas
Get Cart Tax Total programmatically in WooCommerce

How do you get the tax total in WooCommerce in the functions.php page in WordPress, Using :

global $woocommerce;

$discount = $woocommerce->cart->tax_total;

But is not returning any value.

How can I get Cart Tax Total?

Essentially I want the Tax to calculate for the user, but then have it reduced as the customer will pay the taxes on COD.

Full Code below:

add_action( 'woocommerce_calculate_totals', 'action_cart_calculate_totals', 10, 1 );
function action_cart_calculate_totals( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( !WC()->cart->is_empty() ):
        $cart_object->cart_contents_total *= .10 ;

    endif;
}


//Code for removing tax from total collected
function prefix_add_discount_line( $cart ) {

  global $woocommerce;

  $discount = $woocommerce->cart->tax_total;

  $woocommerce->cart->add_fee( __( 'Tax Paid On COD', 'your-text-domain' ) , - $discount );

}
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line' );
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

  1. global $woocommerce; $woocommerce->cart is obsolete for Cart. Use WC()->cart instead.
    Here you can use directly $cart (object) argument instead…
  2. The correct property is taxes instead of tax_total.
  3. Is better to use WC_Cart get_taxes() method intead to be compatible with WooCommerce version 3.0+

To achieve what you are trying to Your code is going to be:

// For Woocommerce 2.5+ (2.6.x and 3.0)
add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line', 10, 1 );
function prefix_add_discount_line( $cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    $discount = 0;
    // Get the unformated taxes array
    $taxes = $cart->get_taxes(); 
    // Add each taxes to $discount
    foreach($taxes as $tax) $discount += $tax;

    // Applying a discount if not null or equal to zero
    if ($discount > 0 && ! empty($discount) )
        $cart->add_fee( __( 'Tax Paid On COD', 'your-text-domain' ) , - $discount );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

about 4 years ago · Santiago Trujillo Denunciar

0

You are using wrong function name. Correct function is as below :-

WC()->cart->get_tax_totals( );

Instead of using $woocommerce->cart->tax_total; to get cart total tax, you can do this by subtracting cart total excluding tax from cart total.

You can do this by following code :-

$total_tax = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total() ) ) - WC()->cart->get_total_ex_tax();

If you want to get array for all taxes then you can get through below code :-

WC()->cart->get_taxes( );
about 4 years ago · Santiago Trujillo Denunciar

0

We can use this function taht worked for me.

WC()->cart->get_total_tax();
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda