WC_REST_Taxes_V1_Controller::create_or_update_tax()protectedWC 1.0

Take tax data from the request and return the updated or newly created rate.

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

Хуков нет.

Возвращает

Объект.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->create_or_update_tax( $request, $current );
$request(WP_REST_Request) (обязательный)
Full details about the request.
$current(stdClass|null)
Existing tax object.
По умолчанию: null

Код WC_REST_Taxes_V1_Controller::create_or_update_tax() WC 8.7.0

protected function create_or_update_tax( $request, $current = null ) {
	$id     = absint( isset( $request['id'] ) ? $request['id'] : 0 );
	$data   = array();
	$fields = array(
		'tax_rate_country',
		'tax_rate_state',
		'tax_rate',
		'tax_rate_name',
		'tax_rate_priority',
		'tax_rate_compound',
		'tax_rate_shipping',
		'tax_rate_order',
		'tax_rate_class',
	);

	foreach ( $fields as $field ) {
		// Keys via API differ from the stored names returned by _get_tax_rate.
		$key = 'tax_rate' === $field ? 'rate' : str_replace( 'tax_rate_', '', $field );

		// Remove data that was not posted.
		if ( ! isset( $request[ $key ] ) ) {
			continue;
		}

		// Test new data against current data.
		if ( $current && $current->$field === $request[ $key ] ) {
			continue;
		}

		// Add to data array.
		switch ( $key ) {
			case 'tax_rate_priority':
			case 'tax_rate_compound':
			case 'tax_rate_shipping':
			case 'tax_rate_order':
				$data[ $field ] = absint( $request[ $key ] );
				break;
			case 'tax_rate_class':
				$data[ $field ] = 'standard' !== $request['tax_rate_class'] ? $request['tax_rate_class'] : '';
				break;
			default:
				$data[ $field ] = wc_clean( $request[ $key ] );
				break;
		}
	}

	if ( ! $id ) {
		$id = WC_Tax::_insert_tax_rate( $data );
	} elseif ( $data ) {
		WC_Tax::_update_tax_rate( $id, $data );
	}

	// Add locales.
	if ( ! empty( $request['postcode'] ) ) {
		WC_Tax::_update_tax_rate_postcodes( $id, wc_clean( $request['postcode'] ) );
	}
	if ( ! empty( $request['city'] ) ) {
		WC_Tax::_update_tax_rate_cities( $id, wc_clean( $request['city'] ) );
	}

	return WC_Tax::_get_tax_rate( $id, OBJECT );
}