wc_gzd_get_cart_tax_share

Calculates tax share for shipping/fees.

Description

(array) wc_gzd_get_cart_tax_share( (string) $type = 'shipping' ); 

Returns (array)

Parameters (1)

0. $type — Optional. (string) => 'shipping'
The type.

Usage

  1. if ( !function_exists( 'wc_gzd_get_cart_tax_share' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-germanized/includes/wc-gzd-cart-functions.php'; 
  3.  
  4. // The type. 
  5. $type = 'shipping'; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wc_gzd_get_cart_tax_share($type); 
  9.  

Defined (1)

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

/includes/wc-gzd-cart-functions.php  
  1. function wc_gzd_get_cart_tax_share( $type = 'shipping' ) { 
  2.  
  3. $cart = WC()->cart->get_cart(); 
  4. $tax_shares = array(); 
  5. $item_totals = 0; 
  6.  
  7. // Get tax classes and tax amounts 
  8. if ( ! empty( $cart ) ) { 
  9.  
  10. foreach ( $cart as $key => $item ) { 
  11.  
  12. $_product = apply_filters( 'woocommerce_cart_item_product', $item[ 'data' ], $item, $key ); 
  13.  
  14. // Dont calculate share if is shipping and product is virtual or vat exception 
  15. if ( $type == 'shipping' && $_product->is_virtual() || ( wc_gzd_get_gzd_product( $_product )->is_virtual_vat_exception() && $type == 'shipping' ) ) 
  16. continue; 
  17.  
  18. $class = $_product->get_tax_class(); 
  19.  
  20. if ( ! isset( $tax_shares[ $class ] ) ) { 
  21. $tax_shares[ $class ] = array(); 
  22. $tax_shares[ $class ][ 'total' ] = 0; 
  23. $tax_shares[ $class ][ key ] = ''; 
  24.  
  25. $tax_shares[ $class ][ 'total' ] += ( $item[ 'line_total' ] + $item[ 'line_tax' ] );  
  26. $tax_shares[ $class ][ key ] =key $item[ 'line_tax_data' ][ 'total' ] ); 
  27. $item_totals += ( $item[ 'line_total' ] + $item[ 'line_tax' ] );  
  28.  
  29. if ( ! empty( $tax_shares ) ) { 
  30.  
  31. $default = ( $item_totals == 0 ? 1 / sizeof( $tax_shares ) : 0 ); 
  32.  
  33. foreach ( $tax_shares as $key => $class ) 
  34. $tax_shares[ $key ][ 'share' ] = ( $item_totals > 0 ? $class[ 'total' ] / $item_totals : $default ); 
  35.  
  36.  
  37. return $tax_shares;