forked from wp-graphql/wp-graphql-woocommerce
/
class-products.php
258 lines (248 loc) · 8.61 KB
/
class-products.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Connection - Products
*
* Registers connections to Product
*
* @package WPGraphQL\Extensions\WooCommerce\Connection
*/
namespace WPGraphQL\Extensions\WooCommerce\Connection;
use WPGraphQL\Extensions\WooCommerce\Data\Factory;
/**
* Class - Products
*/
class Products extends WC_Connection {
/**
* Registers the various connections from other Types to Product
*/
public static function register_connections() {
// From RootQuery.
register_graphql_connection( self::get_connection_config() );
// From Coupon.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Coupon',
'fromFieldName' => 'products',
)
)
);
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Coupon',
'fromFieldName' => 'excludedProducts',
)
)
);
// From Product.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromFieldName' => 'related',
)
)
);
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromFieldName' => 'upsell',
)
)
);
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromFieldName' => 'crossSell',
)
)
);
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'fromFieldName' => 'grouped',
)
)
);
// From Product to ProductVariation.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'Product',
'toType' => 'ProductVariation',
'fromFieldName' => 'variations',
)
)
);
// From ProductCategory.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'ProductCategory',
'fromFieldName' => 'products',
)
)
);
// From ProductTag.
register_graphql_connection(
self::get_connection_config(
array(
'fromType' => 'ProductTag',
'fromFieldName' => 'products',
)
)
);
}
/**
* Given an array of $args, this returns the connection config, merging the provided args
* with the defaults
*
* @access public
* @param array $args - Connection configuration.
*
* @return array
*/
public static function get_connection_config( $args = [] ) {
$defaults = array(
'fromType' => 'RootQuery',
'toType' => 'Product',
'fromFieldName' => 'products',
'connectionArgs' => self::get_connection_args(),
'resolveNode' => function( $id, $args, $context, $info ) {
return Factory::resolve_crud_object( $id, $context );
},
'resolve' => function ( $source, $args, $context, $info ) {
return Factory::resolve_product_connection( $source, $args, $context, $info );
},
);
return array_merge( $defaults, $args );
}
/**
* Returns array of where args
*
* @return array
*/
public static function get_connection_args() {
$args = array(
'slug' => array(
'type' => 'String',
'description' => __( 'Limit result set to products with a specific slug.', 'wp-graphql-woocommerce' ),
),
'status' => array(
'type' => 'String',
'description' => __( 'Limit result set to products assigned a specific status.', 'wp-graphql-woocommerce' ),
),
'type' => array(
'type' => 'ProductTypesEnum',
'description' => __( 'Limit result set to products assigned a specific type.', 'wp-graphql-woocommerce' ),
),
'typeIn' => array(
'type' => array( 'list_of' => 'ProductTypesEnum' ),
'description' => __( 'Limit result set to products assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
),
'typeNotIn' => array(
'type' => array( 'list_of' => 'ProductTypesEnum' ),
'description' => __( 'Limit result set to products not assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
),
'sku' => array(
'type' => 'String',
'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'wp-graphql-woocommerce' ),
),
'featured' => array(
'type' => 'Boolean',
'description' => __( 'Limit result set to featured products.', 'wp-graphql-woocommerce' ),
),
'categoryName' => array(
'type' => 'String',
'description' => __( 'Limit result set to products assigned a specific category name.', 'wp-graphql-woocommerce' ),
),
'categoryNameIn' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Limit result set to products assigned to a group of specific categories by name.', 'wp-graphql-woocommerce' ),
),
'categoryNameNotIn' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Limit result set to products not assigned to a group of specific categories by name.', 'wp-graphql-woocommerce' ),
),
'category' => array(
'type' => 'Int',
'description' => __( 'Limit result set to products assigned a specific category name.', 'wp-graphql-woocommerce' ),
),
'categoryIn' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Limit result set to products assigned to a specific group of category IDs.', 'wp-graphql-woocommerce' ),
),
'categoryNotIn' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Limit result set to products not assigned to a specific group of category IDs.', 'wp-graphql-woocommerce' ),
),
'tag' => array(
'type' => 'String',
'description' => __( 'Limit result set to products assigned a specific tag name.', 'wp-graphql-woocommerce' ),
),
'tagSlugIn' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Limit result set to products assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
),
'tagSlugNotIn' => array(
'type' => array( 'list_of' => 'String' ),
'description' => __( 'Limit result set to products not assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
),
'tagId' => array(
'type' => 'Int',
'description' => __( 'Limit result set to products assigned a specific tag ID.', 'wp-graphql-woocommerce' ),
),
'tagIn' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Limit result set to products assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
),
'tagNotIn' => array(
'type' => array( 'list_of' => 'Int' ),
'description' => __( 'Limit result set to products not assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
),
'shippingClassId' => array(
'type' => 'Int',
'description' => __( 'Limit result set to products assigned a specific shipping class ID.', 'wp-graphql-woocommerce' ),
),
'attribute' => array(
'type' => 'String',
'description' => __( 'Limit result set to products with a specific attribute. Use the taxonomy name/attribute slug.', 'wp-graphql-woocommerce' ),
),
'attributeTerm' => array(
'type' => 'String',
'description' => __( 'Limit result set to products with a specific attribute term ID (required an assigned attribute).', 'wp-graphql-woocommerce' ),
),
'stockStatus' => array(
'type' => 'StockStatusEnum',
'description' => __( 'Limit result set to products in stock or out of stock.', 'wp-graphql-woocommerce' ),
),
'onSale' => array(
'type' => 'Boolean',
'description' => __( 'Limit result set to products on sale.', 'wp-graphql-woocommerce' ),
),
'minPrice' => array(
'type' => 'Float',
'description' => __( 'Limit result set to products based on a minimum price.', 'wp-graphql-woocommerce' ),
),
'maxPrice' => array(
'type' => 'Float',
'description' => __( 'Limit result set to products based on a maximum price.', 'wp-graphql-woocommerce' ),
),
'search' => array(
'type' => 'String',
'description' => __( 'Limit result set to products based on a keyword search.', 'wp-graphql-woocommerce' ),
),
);
if ( wc_tax_enabled() ) {
$args['taxClass'] = array(
'type' => 'TaxClassEnum',
'description' => __( 'Limit result set to products with a specific tax class.', 'wp-graphql-woocommerce' ),
);
}
return array_merge( self::get_shared_connection_args(), $args );
}
}