WC_Order_Item_Product::set_taxespublicWC 10.5.0

Set line taxes and totals for passed in taxes.

Метод класса: WC_Order_Item_Product{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

$WC_Order_Item_Product = new WC_Order_Item_Product();
$WC_Order_Item_Product->set_taxes( $raw_tax_data );
$raw_tax_data(массив) (обязательный)
Raw tax data. 'total' and 'subtotal' should be arrays keyed by tax rate ID, but scalar values (floats/strings) are accepted for legacy compatibility.

Список изменений

С версии 10.5.0 Введена.
С версии 10.5.0 Handles legacy scalar tax values by converting to arrays. When legacy data is detected, attempts to infer tax rate ID from order context.

Код WC_Order_Item_Product::set_taxes() WC 11.0.1

public function set_taxes( $raw_tax_data ) {
	$raw_tax_data = maybe_unserialize( $raw_tax_data );
	$tax_data     = array(
		'total'    => array(),
		'subtotal' => array(),
	);
	if ( ! empty( $raw_tax_data['total'] ) && ! empty( $raw_tax_data['subtotal'] ) ) {
		$subtotal = $raw_tax_data['subtotal'];
		$total    = $raw_tax_data['total'];

		// Handle legacy data where total/subtotal might be floats/strings instead of arrays.
		// Convert scalar values to array format to preserve the tax amount.
		$has_legacy_data = ! is_array( $subtotal ) || ! is_array( $total );

		if ( $has_legacy_data ) {
			$order = $this->get_order();
			if ( ! is_array( $subtotal ) ) {
				$subtotal = $this->convert_legacy_tax_value_to_array( $subtotal, $order );
			}
			if ( ! is_array( $total ) ) {
				$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-product',
					'order_item_id' => $this->get_id(),
					'order_id'      => $order ? $order->get_id() : 0,
				)
			);
		}

		$tax_data['subtotal'] = array_map( 'wc_format_decimal', $subtotal );
		$tax_data['total']    = array_map( 'wc_format_decimal', $total );

		// Subtotal cannot be less than total!
		if ( NumberUtil::array_sum( $tax_data['subtotal'] ) < NumberUtil::array_sum( $tax_data['total'] ) ) {
			$tax_data['subtotal'] = $tax_data['total'];
		}
	}
	$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'] ) );
		$this->set_subtotal_tax( NumberUtil::array_sum( $tax_data['subtotal'] ) );
	} else {
		$this->set_total_tax( NumberUtil::array_sum( array_map( 'wc_round_tax_total', $tax_data['total'] ) ) );
		$this->set_subtotal_tax( NumberUtil::array_sum( array_map( 'wc_round_tax_total', $tax_data['subtotal'] ) ) );
	}
}