2024-05-17 00:03:10 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - createTaxClass
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for creating a tax class.
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
2024-05-21 00:57:56 -04:00
|
|
|
* @since 0.20.0
|
2024-05-17 00:03:10 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class - Tax_Class_Create
|
|
|
|
|
*/
|
|
|
|
|
class Tax_Class_Create {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'createTaxClass',
|
|
|
|
|
[
|
|
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
|
|
|
|
'mutateAndGetPayload' => self::mutate_and_get_payload(),
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
|
|
|
|
return [
|
|
|
|
|
'name' => [
|
|
|
|
|
'type' => [ 'non_null' => 'String' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Name of the tax class.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
'slug' => [
|
|
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Slug of the tax class.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-05-17 00:03:10 -04:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
|
|
|
|
return [
|
|
|
|
|
'taxClass' => [
|
|
|
|
|
'type' => 'TaxClass',
|
|
|
|
|
'resolve' => static function ( $payload ) {
|
|
|
|
|
return ! empty( $payload['taxClass'] ) ? $payload['taxClass'] : null;
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
|
|
|
|
* @return callable
|
|
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload() {
|
|
|
|
|
return static function ( $input, AppContext $context, ResolveInfo $info ) {
|
|
|
|
|
if ( ! \wc_rest_check_manager_permissions( 'settings', 'create' ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Sorry, you are not allowed to create tax classes.', 'graphql-for-ecommerce' ), \rest_authorization_required_code() );
|
2024-05-17 00:03:10 -04:00
|
|
|
}
|
|
|
|
|
$name = $input['name'];
|
|
|
|
|
$slug = ! empty( $input['slug'] ) ? $input['slug'] : '';
|
|
|
|
|
|
|
|
|
|
$tax_class = \WC_Tax::create_tax_class( $name, $slug );
|
|
|
|
|
|
|
|
|
|
if ( is_wp_error( $tax_class ) ) {
|
|
|
|
|
throw new UserError( $tax_class->get_error_message() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter tax class object before responding.
|
|
|
|
|
*
|
|
|
|
|
* @param array $tax_class The shipping method object.
|
|
|
|
|
* @param array $input Request input.
|
|
|
|
|
*/
|
|
|
|
|
$tax_class = apply_filters( 'graphql_woocommerce_tax_class_create', $tax_class, $input );
|
|
|
|
|
|
|
|
|
|
return [ 'taxClass' => $tax_class ];
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|