WC_Order_Item_Shipping::set_taxes
Set taxes.
This is an array of tax ID keys with total amount values.
Method of the class: WC_Order_Item_Shipping{}
No Hooks.
Returns
null. Nothing (null).
Usage
$WC_Order_Item_Shipping = new WC_Order_Item_Shipping(); $WC_Order_Item_Shipping->set_taxes( $raw_tax_data );
- $raw_tax_data(array) (required)
- Value to set.
Changelog
| Since 10.5.0 | Introduced. |
| Since 10.5.0 | Handles legacy scalar tax values by converting to arrays. |
WC_Order_Item_Shipping::set_taxes() WC Order Item Shipping::set taxes code WC 10.8.1
public function set_taxes( $raw_tax_data ) {
$raw_tax_data = maybe_unserialize( $raw_tax_data );
$tax_data = array(
'total' => array(),
);
if ( isset( $raw_tax_data['total'] ) ) {
$total = $raw_tax_data['total'];
// Handle legacy data where total might be a float/string instead of an array.
if ( ! is_array( $total ) ) {
$order = $this->get_order();
$total = $this->convert_legacy_tax_value_to_array( $total, $order );
// Log legacy data format for debugging purposes.
wc_get_logger()->warning(
sprintf(
/* translators: %d: order item ID */
__( 'Order item #%d contains legacy tax data format. Tax rate ID information is unavailable.', 'woocommerce' ),
$this->get_id()
),
array(
'source' => 'woocommerce-order-item-shipping',
'order_item_id' => $this->get_id(),
'order_id' => $order ? $order->get_id() : 0,
)
);
}
$tax_data['total'] = array_map( 'wc_format_decimal', $total );
} elseif ( ! empty( $raw_tax_data ) && is_array( $raw_tax_data ) ) {
// Older versions just used an array.
$tax_data['total'] = array_map( 'wc_format_decimal', $raw_tax_data );
}
$this->set_prop( 'taxes', $tax_data );
if ( 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' ) ) {
$this->set_total_tax( NumberUtil::array_sum( $tax_data['total'] ) );
} else {
$this->set_total_tax( NumberUtil::array_sum( array_map( 'wc_round_tax_total', $tax_data['total'] ) ) );
}
}