wc_get_price_including_tax

For a given product, and optionally price/qty, work out the price with tax included, based on store settings.

Description

(float) wc_get_price_including_tax( (WC_Product) $product, (array) $args = array() ); 

Returns (float)

Parameters (2)

0. $product (WC_Product)
The product.
1. $args — Optional. (array) => array()
The args.

Usage

  1. if ( !function_exists( 'wc_get_price_including_tax' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-product-functions.php'; 
  3.  
  4. // The product. 
  5. $product = null; 
  6.  
  7. // The args. 
  8. $args = array(); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wc_get_price_including_tax($product, $args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-product-functions.php  
  1. function wc_get_price_including_tax( $product, $args = array() ) { 
  2. $args = wp_parse_args( $args, array( 
  3. 'qty' => '',  
  4. 'price' => '',  
  5. ) ); 
  6.  
  7. $price = '' !== $args['price'] ? max( 0.0, (float) $args['price'] ) : $product->get_price(); 
  8. $qty = '' !== $args['qty'] ? max( 0.0, (float) $args['qty'] ) : 1; 
  9.  
  10. if ( '' === $price ) { 
  11. return ''; 
  12. } elseif ( empty( $qty ) ) { 
  13. return 0.0; 
  14.  
  15. $line_price = $price * $qty; 
  16. $return_price = $line_price; 
  17.  
  18. if ( $product->is_taxable() ) { 
  19. if ( ! wc_prices_include_tax() ) { 
  20. $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); 
  21. $taxes = WC_Tax::calc_tax( $line_price, $tax_rates, false ); 
  22. $tax_amount = WC_Tax::get_tax_total( $taxes ); 
  23. $return_price = round( $line_price + $tax_amount, wc_get_price_decimals() ); 
  24. } else { 
  25. $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); 
  26. $base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) ); 
  27.  
  28. /** 
  29. * If the customer is excempt from VAT, remove the taxes here. 
  30. * Either remove the base or the user taxes depending on woocommerce_adjust_non_base_location_prices setting. 
  31. */ 
  32. if ( ! empty( WC()->customer ) && WC()->customer->get_is_vat_exempt() ) { 
  33. $remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true ); 
  34. $remove_tax = array_sum( $remove_taxes ); 
  35. $return_price = round( $line_price - $remove_tax, wc_get_price_decimals() ); 
  36.  
  37. /** 
  38. * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations. 
  39. * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes. 
  40. * This feature is experimental @since 2.4.7 and may change in the future. Use at your risk. 
  41. */ 
  42. } elseif ( $tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { 
  43. $base_taxes = WC_Tax::calc_tax( $line_price, $base_tax_rates, true ); 
  44. $modded_taxes = WC_Tax::calc_tax( $line_price - array_sum( $base_taxes ), $tax_rates, false ); 
  45. $return_price = round( $line_price - array_sum( $base_taxes ) + array_sum( $modded_taxes ), wc_get_price_decimals() ); 
  46. return apply_filters( 'woocommerce_get_price_including_tax', $return_price, $qty, $product );