2021-07-05 13:51:52 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Mutation - createCoupon
|
|
|
|
|
*
|
|
|
|
|
* Registers mutation for creating 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\Data\Mutation\Coupon_Mutation;
|
|
|
|
|
use WPGraphQL\WooCommerce\Model\Coupon;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Coupon_Create
|
|
|
|
|
*/
|
|
|
|
|
class Coupon_Create {
|
|
|
|
|
/**
|
|
|
|
|
* 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(
|
|
|
|
|
'createCoupon',
|
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 [
|
|
|
|
|
'code' => [
|
|
|
|
|
'type' => [ 'non_null' => 'String' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Coupon code.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'amount' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'Float',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The amount of discount. Should always be numeric, even if setting a percentage.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'discountType' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'DiscountTypeEnum',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Determines the type of discount that will be applied.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'description' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Coupon description.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'dateExpires' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The date the coupon expires, in the site\'s timezone.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'dateExpiresGmt' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'The date the coupon expires, as GMT.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'individualUse' => [
|
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 __( 'If true, the coupon can only be used individually. Other applied coupons will be removed from the cart.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'productIds' => [
|
|
|
|
|
'type' => [ 'list_of' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'List of product IDs the coupon can be used on.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'excludedProductIds' => [
|
|
|
|
|
'type' => [ 'list_of' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'List of product IDs the coupon cannot be used on.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'usageLimit' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'Int',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'How many times the coupon can be used in total.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'usageLimitPerUser' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'Int',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'How many times the coupon can be used per customer.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'limitUsageToXItems' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'Int',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Max number of items in the cart the coupon can be applied to.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'freeShipping' => [
|
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 __( 'If true and if the free shipping method requires a coupon, this coupon will enable free shipping.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'productCategories' => [
|
|
|
|
|
'type' => [ 'list_of' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'List of category IDs the coupon applies to.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'excludedProductCategories' => [
|
|
|
|
|
'type' => [ 'list_of' => 'Int' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'List of category IDs the coupon does not apply to.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'excludeSaleItems' => [
|
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 __( 'If true, this coupon will not be applied to items that have sale prices.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'minimumAmount' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Minimum order amount that needs to be in the cart before coupon applies.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'maximumAmount' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'String',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Maximum order amount allowed when using the coupon.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'emailRestrictions' => [
|
|
|
|
|
'type' => [ 'list_of' => 'String' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'List of email addresses that can use this coupon.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'metaData' => [
|
|
|
|
|
'type' => [ 'list_of' => 'MetaDataInput' ],
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Meta data.', '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() {
|
2022-08-26 14:53:36 -04:00
|
|
|
return [
|
|
|
|
|
'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 new Coupon( $payload['id'] );
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
],
|
|
|
|
|
'code' => [
|
2021-07-05 13:51:52 -04:00
|
|
|
'type' => 'String',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $payload ) {
|
2021-07-05 13:51:52 -04:00
|
|
|
return $payload['code'];
|
|
|
|
|
},
|
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
|
|
|
if ( ! empty( $input['id'] ) ) {
|
|
|
|
|
$coupon_id = Utils::get_database_id_from_id( $input['id'] );
|
|
|
|
|
} else {
|
|
|
|
|
$coupon_id = 0;
|
|
|
|
|
}
|
2021-07-05 13:51:52 -04:00
|
|
|
|
2024-11-06 15:00:11 -05:00
|
|
|
if ( false === $coupon_id ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Coupon ID provided is invalid. Please check input and try again.', 'graphql-for-ecommerce' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon = new \WC_Coupon( $coupon_id );
|
|
|
|
|
|
|
|
|
|
if ( 0 === $coupon_id && ! wc_rest_check_post_permissions( 'shop_coupon', 'create' ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Sorry, you are not allowed to create resources.', 'graphql-for-ecommerce' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( 0 !== $coupon_id && ! wc_rest_check_post_permissions( 'shop_coupon', 'edit', $coupon_id ) ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'Sorry, you are not allowed to edit this resource.', 'graphql-for-ecommerce' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon_args = Coupon_Mutation::prepare_args( $input );
|
|
|
|
|
|
|
|
|
|
foreach ( $coupon_args as $key => $value ) {
|
|
|
|
|
switch ( $key ) {
|
|
|
|
|
case 'code':
|
|
|
|
|
$coupon_code = wc_format_coupon_code( $value );
|
|
|
|
|
$id = $coupon->get_id() ? $coupon->get_id() : 0;
|
|
|
|
|
$id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
|
|
|
|
|
|
|
|
|
|
if ( $id_from_code ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
throw new UserError( __( 'The coupon code already exists', 'graphql-for-ecommerce' ) );
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$coupon->set_code( $coupon_code );
|
|
|
|
|
break;
|
|
|
|
|
case 'meta_data':
|
|
|
|
|
if ( is_array( $value ) ) {
|
|
|
|
|
foreach ( $value as $meta ) {
|
|
|
|
|
$coupon->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'description':
|
|
|
|
|
$coupon->set_description( wp_filter_post_kses( $value ) );
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2022-08-26 14:53:36 -04:00
|
|
|
if ( is_callable( [ $coupon, "set_{$key}" ] ) ) {
|
2021-07-05 13:51:52 -04:00
|
|
|
$coupon->{"set_{$key}"}( $value );
|
|
|
|
|
}
|
|
|
|
|
break;
|
2022-02-04 15:00:44 -05:00
|
|
|
}//end switch
|
|
|
|
|
}//end foreach
|
2021-07-05 13:51:52 -04:00
|
|
|
|
2022-08-26 14:53:36 -04:00
|
|
|
return [ 'id' => $coupon->save() ];
|
2021-07-05 13:51:52 -04:00
|
|
|
}
|
|
|
|
|
}
|