• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql-woocommerce / 11714183904

07 Nov 2024 12:23AM UTC coverage: 83.665% (-0.8%) from 84.451%
11714183904

push

github

web-flow
devops: Name officially changed to "WPGraphQL for WooCommerce" (#900)

* devops: Name officially changed to "WPGraphQL for WooCommerce"

* devops: GA workflow environment updated

* devops: GA workflow environment updated

* devops: composer-git-hooks downgraded to "2.8.5"

* devops: Unstable session manager tests skipped

* chore: cleanup applied

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

* devops: Docker configurations updated

2 of 8 new or added lines in 5 files covered. (25.0%)

268 existing lines in 20 files now uncovered.

12431 of 14858 relevant lines covered (83.67%)

71.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.38
/includes/mutation/class-coupon-create.php
1
<?php
2
/**
3
 * Mutation - createCoupon
4
 *
5
 * Registers mutation for creating an coupon.
6
 *
7
 * @package WPGraphQL\WooCommerce\Mutation
8
 * @since 0.9.0
9
 */
10

11
namespace WPGraphQL\WooCommerce\Mutation;
12

13
use GraphQL\Error\UserError;
14
use GraphQL\Type\Definition\ResolveInfo;
15
use WPGraphQL\AppContext;
16
use WPGraphQL\Utils\Utils;
17
use WPGraphQL\WooCommerce\Data\Mutation\Coupon_Mutation;
18
use WPGraphQL\WooCommerce\Model\Coupon;
19

20
/**
21
 * Class Coupon_Create
22
 */
23
class Coupon_Create {
24
        /**
25
         * Registers mutation
26
         *
27
         * @return void
28
         */
29
        public static function register_mutation() {
30
                register_graphql_mutation(
141✔
31
                        'createCoupon',
141✔
32
                        [
141✔
33
                                'inputFields'         => self::get_input_fields(),
141✔
34
                                'outputFields'        => self::get_output_fields(),
141✔
35
                                'mutateAndGetPayload' => [ self::class, 'mutate_and_get_payload' ],
141✔
36
                        ]
141✔
37
                );
141✔
38
        }
39

40
        /**
41
         * Defines the mutation input field configuration
42
         *
43
         * @return array
44
         */
45
        public static function get_input_fields() {
46
                return [
141✔
47
                        'code'                      => [
141✔
48
                                'type'        => [ 'non_null' => 'String' ],
141✔
49
                                'description' => __( 'Coupon code.', 'wp-graphql-woocommerce' ),
141✔
50
                        ],
141✔
51
                        'amount'                    => [
141✔
52
                                'type'        => 'Float',
141✔
53
                                'description' => __( 'The amount of discount. Should always be numeric, even if setting a percentage.', 'wp-graphql-woocommerce' ),
141✔
54
                        ],
141✔
55
                        'discountType'              => [
141✔
56
                                'type'        => 'DiscountTypeEnum',
141✔
57
                                'description' => __( 'Determines the type of discount that will be applied.', 'wp-graphql-woocommerce' ),
141✔
58
                        ],
141✔
59
                        'description'               => [
141✔
60
                                'type'        => 'String',
141✔
61
                                'description' => __( 'Coupon description.', 'wp-graphql-woocommerce' ),
141✔
62
                        ],
141✔
63
                        'dateExpires'               => [
141✔
64
                                'type'        => 'String',
141✔
65
                                'description' => __( 'The date the coupon expires, in the site\'s timezone.', 'wp-graphql-woocommerce' ),
141✔
66
                        ],
141✔
67
                        'dateExpiresGmt'            => [
141✔
68
                                'type'        => 'String',
141✔
69
                                'description' => __( 'The date the coupon expires, as GMT.', 'wp-graphql-woocommerce' ),
141✔
70
                        ],
141✔
71
                        'individualUse'             => [
141✔
72
                                'type'        => 'Boolean',
141✔
73
                                'description' => __( 'If true, the coupon can only be used individually. Other applied coupons will be removed from the cart.', 'wp-graphql-woocommerce' ),
141✔
74
                        ],
141✔
75
                        'productIds'                => [
141✔
76
                                'type'        => [ 'list_of' => 'Int' ],
141✔
77
                                'description' => __( 'List of product IDs the coupon can be used on.', 'wp-graphql-woocommerce' ),
141✔
78
                        ],
141✔
79
                        'excludedProductIds'        => [
141✔
80
                                'type'        => [ 'list_of' => 'Int' ],
141✔
81
                                'description' => __( 'List of product IDs the coupon cannot be used on.', 'wp-graphql-woocommerce' ),
141✔
82
                        ],
141✔
83
                        'usageLimit'                => [
141✔
84
                                'type'        => 'Int',
141✔
85
                                'description' => __( 'How many times the coupon can be used in total.', 'wp-graphql-woocommerce' ),
141✔
86
                        ],
141✔
87
                        'usageLimitPerUser'         => [
141✔
88
                                'type'        => 'Int',
141✔
89
                                'description' => __( 'How many times the coupon can be used per customer.', 'wp-graphql-woocommerce' ),
141✔
90
                        ],
141✔
91
                        'limitUsageToXItems'        => [
141✔
92
                                'type'        => 'Int',
141✔
93
                                'description' => __( 'Max number of items in the cart the coupon can be applied to.', 'wp-graphql-woocommerce' ),
141✔
94
                        ],
141✔
95
                        'freeShipping'              => [
141✔
96
                                'type'        => 'Boolean',
141✔
97
                                'description' => __( 'If true and if the free shipping method requires a coupon, this coupon will enable free shipping.', 'wp-graphql-woocommerce' ),
141✔
98
                        ],
141✔
99
                        'productCategories'         => [
141✔
100
                                'type'        => [ 'list_of' => 'Int' ],
141✔
101
                                'description' => __( 'List of category IDs the coupon applies to.', 'wp-graphql-woocommerce' ),
141✔
102
                        ],
141✔
103
                        'excludedProductCategories' => [
141✔
104
                                'type'        => [ 'list_of' => 'Int' ],
141✔
105
                                'description' => __( 'List of category IDs the coupon does not apply to.', 'wp-graphql-woocommerce' ),
141✔
106
                        ],
141✔
107
                        'excludeSaleItems'          => [
141✔
108
                                'type'        => 'Boolean',
141✔
109
                                'description' => __( 'If true, this coupon will not be applied to items that have sale prices.', 'wp-graphql-woocommerce' ),
141✔
110
                        ],
141✔
111
                        'minimumAmount'             => [
141✔
112
                                'type'        => 'String',
141✔
113
                                'description' => __( 'Minimum order amount that needs to be in the cart before coupon applies.', 'wp-graphql-woocommerce' ),
141✔
114
                        ],
141✔
115
                        'maximumAmount'             => [
141✔
116
                                'type'        => 'String',
141✔
117
                                'description' => __( 'Maximum order amount allowed when using the coupon.', 'wp-graphql-woocommerce' ),
141✔
118
                        ],
141✔
119
                        'emailRestrictions'         => [
141✔
120
                                'type'        => [ 'list_of' => 'String' ],
141✔
121
                                'description' => __( 'List of email addresses that can use this coupon.', 'wp-graphql-woocommerce' ),
141✔
122
                        ],
141✔
123
                        'metaData'                  => [
141✔
124
                                'type'        => [ 'list_of' => 'MetaDataInput' ],
141✔
125
                                'description' => __( 'Meta data.', 'wp-graphql-woocommerce' ),
141✔
126
                        ],
141✔
127
                ];
141✔
128
        }
129

130
        /**
131
         * Defines the mutation output field configuration
132
         *
133
         * @return array
134
         */
135
        public static function get_output_fields() {
136
                return [
141✔
137
                        'coupon' => [
141✔
138
                                'type'    => 'Coupon',
141✔
139
                                'resolve' => static function ( $payload ) {
141✔
140
                                        return new Coupon( $payload['id'] );
2✔
141
                                },
141✔
142
                        ],
141✔
143
                        'code'   => [
141✔
144
                                'type'    => 'String',
141✔
145
                                'resolve' => static function ( $payload ) {
141✔
146
                                        return $payload['code'];
×
147
                                },
141✔
148
                        ],
141✔
149
                ];
141✔
150
        }
151

152
        /**
153
         * Defines the mutation data modification closure.
154
         *
155
         * @param array                                $input    Mutation input.
156
         * @param \WPGraphQL\AppContext                $context  AppContext instance.
157
         * @param \GraphQL\Type\Definition\ResolveInfo $info     ResolveInfo instance. Can be
158
         * use to get info about the current node in the GraphQL tree.
159
         *
160
         * @throws \GraphQL\Error\UserError Invalid ID provided | Lack of capabilities.
161
         *
162
         * @return array
163
         */
164
        public static function mutate_and_get_payload( $input, AppContext $context, ResolveInfo $info ) {
165
                // Retrieve order ID.
166
                if ( ! empty( $input['id'] ) ) {
2✔
167
                        $coupon_id = Utils::get_database_id_from_id( $input['id'] );
1✔
168
                } else {
169
                        $coupon_id = 0;
1✔
170
                }
171

172
                if ( false === $coupon_id ) {
2✔
UNCOV
173
                        throw new UserError( __( 'Coupon ID provided is invalid. Please check input and try again.', 'wp-graphql-woocommerce' ) );
×
174
                }
175

176
                $coupon = new \WC_Coupon( $coupon_id );
2✔
177

178
                if ( 0 === $coupon_id && ! wc_rest_check_post_permissions( 'shop_coupon', 'create' ) ) {
2✔
179
                        throw new UserError( __( 'Sorry, you are not allowed to create resources.', 'wp-graphql-woocommerce' ) );
1✔
180
                }
181

182
                if ( 0 !== $coupon_id && ! wc_rest_check_post_permissions( 'shop_coupon', 'edit', $coupon_id ) ) {
2✔
183
                        throw new UserError( __( 'Sorry, you are not allowed to edit this resource.', 'wp-graphql-woocommerce' ) );
1✔
184
                }
185

186
                $coupon_args = Coupon_Mutation::prepare_args( $input );
2✔
187

188
                foreach ( $coupon_args as $key => $value ) {
2✔
189
                        switch ( $key ) {
190
                                case 'code':
2✔
191
                                        $coupon_code  = wc_format_coupon_code( $value );
2✔
192
                                        $id           = $coupon->get_id() ? $coupon->get_id() : 0;
2✔
193
                                        $id_from_code = wc_get_coupon_id_by_code( $coupon_code, $id );
2✔
194

195
                                        if ( $id_from_code ) {
2✔
UNCOV
196
                                                throw new UserError( __( 'The coupon code already exists', 'wp-graphql-woocommerce' ) );
×
197
                                        }
198

199
                                        $coupon->set_code( $coupon_code );
2✔
200
                                        break;
2✔
201
                                case 'meta_data':
2✔
UNCOV
202
                                        if ( is_array( $value ) ) {
×
UNCOV
203
                                                foreach ( $value as $meta ) {
×
204
                                                        $coupon->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' );
×
205
                                                }
206
                                        }
UNCOV
207
                                        break;
×
208
                                case 'description':
2✔
209
                                        $coupon->set_description( wp_filter_post_kses( $value ) );
×
UNCOV
210
                                        break;
×
211
                                default:
212
                                        if ( is_callable( [ $coupon, "set_{$key}" ] ) ) {
2✔
213
                                                $coupon->{"set_{$key}"}( $value );
2✔
214
                                        }
215
                                        break;
2✔
216
                        }//end switch
217
                }//end foreach
218

219
                return [ 'id' => $coupon->save() ];
2✔
220
        }
221
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc