2021-07-05 13:51:52 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - deleteCoupon
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for deleting an coupon.
|
|
|
|
|
*
|
|
|
|
|
* @package WPGraphQL\WooCommerce\Mutation
|
|
|
|
|
* @since 0.9.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace WPGraphQL\WooCommerce\Mutation;
|
|
|
|
|
|
|
|
|
|
use GraphQL\Error\UserError;
|
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
|
|
|
use WPGraphQL\AppContext;
|
2024-11-06 15:00:11 -05:00
|
|
|
use WPGraphQL\Utils\Utils;
|
2021-07-05 13:51:52 -04:00
|
|
|
use WPGraphQL\WooCommerce\Model\Coupon;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Coupon_Delete
|
|
|
|
|
*/
|
|
|
|
|
class Coupon_Delete {
|
|
|
|
|
/**
|
|
|
|
|
* Registers mutation
|
2023-06-13 23:17:02 +03:00
|
|
|
*
|
|
|
|
|
* @return void
|
2021-07-05 13:51:52 -04:00
|
|
|
*/
|
|
|
|
|
public static function register_mutation() {
|
|
|
|
|
register_graphql_mutation(
|
|
|
|
|
'deleteCoupon',
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2021-07-05 13:51:52 -04:00
|
|
|
'inputFields' => self::get_input_fields(),
|
|
|
|
|
'outputFields' => self::get_output_fields(),
|
2023-07-20 00:11:25 +03:00
|
|
|
'mutateAndGetPayload' => [ self::class, 'mutate_and_get_payload' ],
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2021-07-05 13:51:52 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation input field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_input_fields() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'id' => [
|
|
|
|
|
'type' => [ 'non_null' => 'ID' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Unique identifier for the object.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'forceDelete' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'Boolean',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Delete the object. Set to "false" by default.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
];
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation output field configuration
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public static function get_output_fields() {
|
|
|
|
|
return array_merge(
|
|
|
|
|
Coupon_Create::get_output_fields(),
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
|
|
|
|
'coupon' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'Coupon',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2021-07-05 13:51:52 -04:00
|
|
|
return ! empty( $payload['coupon'] ) ? $payload['coupon'] : null;
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
]
|
2021-07-05 13:51:52 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines the mutation data modification closure.
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @param array $input Mutation input.
|
|
|
|
|
* @param \WPGraphQL\AppContext $context AppContext instance.
|
|
|
|
|
* @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo instance. Can be
|
2021-07-05 13:51:52 -04:00
|
|
|
* use to get info about the current node in the GraphQL tree.
|
|
|
|
|
*
|
2023-07-19 23:01:50 +03:00
|
|
|
* @throws \GraphQL\Error\UserError Invalid ID provided | Lack of capabilities.
|
2021-07-05 13:51:52 -04:00
|
|
|
*
|
2023-06-13 23:17:02 +03:00
|
|
|
* @return array
|
2021-07-05 13:51:52 -04:00
|
|
|
*/
|
|
|
|
|
public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
|
|
|
|
|
// Retrieve order ID.
|
2024-11-06 15:00:11 -05:00
|
|
|
$coupon_id = Utils::get_database_id_from_id( $input['id'] );
|
|
|
|
|
if ( empty( $coupon_id ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Coupon ID provided is missing or invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
2024-11-06 15:00:11 -05:00
|
|
|
|
2021-07-05 13:51:52 -04:00
|
|
|
$coupon = new Coupon( $coupon_id );
|
|
|
|
|
|
|
|
|
|
if ( ! $coupon->ID ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Invalid ID.', 'graphql-for-ecommerce' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( ! wc_rest_check_post_permissions( 'shop_coupon', 'delete', $coupon->ID ) ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
/**
|
|
|
|
|
* Get coupon post type.
|
|
|
|
|
*
|
|
|
|
|
* @var \WP_Post_Type $post_type_object
|
|
|
|
|
*/
|
|
|
|
|
$post_type_object = get_post_type_object( 'shop_coupon' );
|
2021-07-05 13:51:52 -04:00
|
|
|
throw new UserError(
|
|
|
|
|
sprintf(
|
|
|
|
|
/* translators: %s: post type */
|
2026-06-30 10:16:21 -04:00
|
|
|
__( 'Sorry, you are not allowed to delete %s.', 'graphql-for-ecommerce' ),
|
2023-06-13 23:17:02 +03:00
|
|
|
lcfirst( $post_type_object->label )
|
2021-07-05 13:51:52 -04:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fields_to_cache = $info->getFieldSelection( 2 );
|
|
|
|
|
foreach ( $fields_to_cache['coupon'] as $field => $_ ) {
|
2023-06-13 23:17:02 +03:00
|
|
|
$cached = $coupon->$field;
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$force_delete = isset( $input['forceDelete'] ) ? $input['forceDelete'] : false;
|
|
|
|
|
$coupon->delete( $force_delete );
|
|
|
|
|
|
|
|
|
|
return compact( 'coupon' );
|
|
|
|
|
}
|
|
|
|
|
}
|