WC_API_Taxes::create_tax_class()publicWC 2.5.0

Create a tax class.

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

Хуки из метода

Возвращает

Массив|WP_Error.

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

$WC_API_Taxes = new WC_API_Taxes();
$WC_API_Taxes->create_tax_class( $data );
$data(массив) (обязательный)
-

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

С версии 2.5.0 Введена.

Код WC_API_Taxes::create_tax_class() WC 8.7.0

public function create_tax_class( $data ) {
	try {
		if ( ! isset( $data['tax_class'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_tax_class_data', sprintf( __( 'No %1$s data specified to create %1$s', 'woocommerce' ), 'tax_class' ), 400 );
		}

		// Check permissions
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
			throw new WC_API_Exception( 'woocommerce_api_user_cannot_create_tax_class', __( 'You do not have permission to create tax classes', 'woocommerce' ), 401 );
		}

		$data = $data['tax_class'];

		if ( empty( $data['name'] ) ) {
			throw new WC_API_Exception( 'woocommerce_api_missing_tax_class_name', sprintf( __( 'Missing parameter %s', 'woocommerce' ), 'name' ), 400 );
		}

		$name      = sanitize_text_field( $data['name'] );
		$tax_class = WC_Tax::create_tax_class( $name );

		if ( is_wp_error( $tax_class ) ) {
			return new WP_Error( 'woocommerce_api_' . $tax_class->get_error_code(), $tax_class->get_error_message(), 401 );
		}

		do_action( 'woocommerce_api_create_tax_class', $tax_class['slug'], $data );

		$this->server->send_status( 201 );

		return array(
			'tax_class' => $tax_class,
		);
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}