WC_Shipping_Method::get_taxes_per_item
Calc taxes per item being shipping in costs array.
Method of the class: WC_Shipping_Method{}
No Hooks.
Returns
Array. of taxes
Usage
// protected - for code of main (parent) or child class $result = $this->get_taxes_per_item( $costs );
- $costs(array) (required)
- Costs.
Changelog
| Since 2.6.0 | Introduced. |
WC_Shipping_Method::get_taxes_per_item() WC Shipping Method::get taxes per item code WC 10.7.0
protected function get_taxes_per_item( $costs ) {
$taxes = array();
// If we have an array of costs we can look up each items tax class and add tax accordingly.
if ( is_array( $costs ) ) {
$cart = WC()->cart->get_cart();
foreach ( $costs as $cost_key => $amount ) {
if ( ! isset( $cart[ $cost_key ] ) ) {
continue;
}
$cart_item_data = $cart[ $cost_key ]['data'];
if ( is_object( $cart_item_data ) && is_callable( array( $cart_item_data, 'get_tax_class' ) ) ) {
$tax_class = $cart_item_data->get_tax_class();
} else {
$tax_class = null;
}
$item_tax_rates = WC_Tax::get_shipping_tax_rates( $tax_class );
$item_taxes = WC_Tax::calc_shipping_tax( $amount, $item_tax_rates );
// Sum the item taxes.
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
}
}
// Add any cost for the order - order costs are in the key 'order'.
if ( isset( $costs['order'] ) ) {
$order_tax_rates = WC_Tax::get_shipping_tax_rates();
$item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], $order_tax_rates );
// Sum the item taxes.
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
}
}
}
return $taxes;
}