Skip to content

Commit 8a68cf9

Browse files
committed
Bug related to resolving product connections by taxonomies fixed.
1 parent 142c482 commit 8a68cf9

File tree

3 files changed

+254
-75
lines changed

3 files changed

+254
-75
lines changed

includes/connection/class-products.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -158,115 +158,115 @@ public static function get_connection_config( $args = [] ) {
158158
*/
159159
public static function get_connection_args() {
160160
$args = array(
161-
'slug' => array(
161+
'slug' => array(
162162
'type' => 'String',
163163
'description' => __( 'Limit result set to products with a specific slug.', 'wp-graphql-woocommerce' ),
164164
),
165-
'status' => array(
165+
'status' => array(
166166
'type' => 'String',
167167
'description' => __( 'Limit result set to products assigned a specific status.', 'wp-graphql-woocommerce' ),
168168
),
169-
'type' => array(
169+
'type' => array(
170170
'type' => 'ProductTypesEnum',
171171
'description' => __( 'Limit result set to products assigned a specific type.', 'wp-graphql-woocommerce' ),
172172
),
173-
'typeIn' => array(
173+
'typeIn' => array(
174174
'type' => array( 'list_of' => 'ProductTypesEnum' ),
175175
'description' => __( 'Limit result set to products assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
176176
),
177-
'typeNotIn' => array(
177+
'typeNotIn' => array(
178178
'type' => array( 'list_of' => 'ProductTypesEnum' ),
179179
'description' => __( 'Limit result set to products not assigned to a group of specific types.', 'wp-graphql-woocommerce' ),
180180
),
181-
'sku' => array(
181+
'sku' => array(
182182
'type' => 'String',
183183
'description' => __( 'Limit result set to products with specific SKU(s). Use commas to separate.', 'wp-graphql-woocommerce' ),
184184
),
185-
'featured' => array(
185+
'featured' => array(
186186
'type' => 'Boolean',
187187
'description' => __( 'Limit result set to featured products.', 'wp-graphql-woocommerce' ),
188188
),
189-
'categoryName' => array(
189+
'category' => array(
190190
'type' => 'String',
191191
'description' => __( 'Limit result set to products assigned a specific category name.', 'wp-graphql-woocommerce' ),
192192
),
193-
'categoryNameIn' => array(
193+
'categoryIn' => array(
194194
'type' => array( 'list_of' => 'String' ),
195195
'description' => __( 'Limit result set to products assigned to a group of specific categories by name.', 'wp-graphql-woocommerce' ),
196196
),
197-
'categoryNameNotIn' => array(
197+
'categoryNotIn' => array(
198198
'type' => array( 'list_of' => 'String' ),
199199
'description' => __( 'Limit result set to products not assigned to a group of specific categories by name.', 'wp-graphql-woocommerce' ),
200200
),
201-
'category' => array(
201+
'categoryId' => array(
202202
'type' => 'Int',
203203
'description' => __( 'Limit result set to products assigned a specific category name.', 'wp-graphql-woocommerce' ),
204204
),
205-
'categoryIn' => array(
205+
'categoryIdIn' => array(
206206
'type' => array( 'list_of' => 'Int' ),
207207
'description' => __( 'Limit result set to products assigned to a specific group of category IDs.', 'wp-graphql-woocommerce' ),
208208
),
209-
'categoryNotIn' => array(
209+
'categoryIdNotIn' => array(
210210
'type' => array( 'list_of' => 'Int' ),
211211
'description' => __( 'Limit result set to products not assigned to a specific group of category IDs.', 'wp-graphql-woocommerce' ),
212212
),
213-
'tag' => array(
213+
'tag' => array(
214214
'type' => 'String',
215215
'description' => __( 'Limit result set to products assigned a specific tag name.', 'wp-graphql-woocommerce' ),
216216
),
217-
'tagSlugIn' => array(
217+
'tagIn' => array(
218218
'type' => array( 'list_of' => 'String' ),
219-
'description' => __( 'Limit result set to products assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
219+
'description' => __( 'Limit result set to products assigned to a specific group of tags by name.', 'wp-graphql-woocommerce' ),
220220
),
221-
'tagSlugNotIn' => array(
221+
'tagNotIn' => array(
222222
'type' => array( 'list_of' => 'String' ),
223-
'description' => __( 'Limit result set to products not assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
223+
'description' => __( 'Limit result set to products not assigned to a specific group of tags by name.', 'wp-graphql-woocommerce' ),
224224
),
225-
'tagId' => array(
225+
'tagId' => array(
226226
'type' => 'Int',
227227
'description' => __( 'Limit result set to products assigned a specific tag ID.', 'wp-graphql-woocommerce' ),
228228
),
229-
'tagIn' => array(
229+
'tagIdIn' => array(
230230
'type' => array( 'list_of' => 'Int' ),
231231
'description' => __( 'Limit result set to products assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
232232
),
233-
'tagNotIn' => array(
233+
'tagIdNotIn' => array(
234234
'type' => array( 'list_of' => 'Int' ),
235235
'description' => __( 'Limit result set to products not assigned to a specific group of tag IDs.', 'wp-graphql-woocommerce' ),
236236
),
237-
'shippingClassId' => array(
237+
'shippingClassId' => array(
238238
'type' => 'Int',
239239
'description' => __( 'Limit result set to products assigned a specific shipping class ID.', 'wp-graphql-woocommerce' ),
240240
),
241-
'attribute' => array(
241+
'attribute' => array(
242242
'type' => 'String',
243243
'description' => __( 'Limit result set to products with a specific attribute. Use the taxonomy name/attribute slug.', 'wp-graphql-woocommerce' ),
244244
),
245-
'attributeTerm' => array(
245+
'attributeTerm' => array(
246246
'type' => 'String',
247247
'description' => __( 'Limit result set to products with a specific attribute term ID (required an assigned attribute).', 'wp-graphql-woocommerce' ),
248248
),
249-
'stockStatus' => array(
249+
'stockStatus' => array(
250250
'type' => array( 'list_of' => 'StockStatusEnum' ),
251251
'description' => __( 'Limit result set to products in stock or out of stock.', 'wp-graphql-woocommerce' ),
252252
),
253-
'onSale' => array(
253+
'onSale' => array(
254254
'type' => 'Boolean',
255255
'description' => __( 'Limit result set to products on sale.', 'wp-graphql-woocommerce' ),
256256
),
257-
'minPrice' => array(
257+
'minPrice' => array(
258258
'type' => 'Float',
259259
'description' => __( 'Limit result set to products based on a minimum price.', 'wp-graphql-woocommerce' ),
260260
),
261-
'maxPrice' => array(
261+
'maxPrice' => array(
262262
'type' => 'Float',
263263
'description' => __( 'Limit result set to products based on a maximum price.', 'wp-graphql-woocommerce' ),
264264
),
265-
'search' => array(
265+
'search' => array(
266266
'type' => 'String',
267267
'description' => __( 'Limit result set to products based on a keyword search.', 'wp-graphql-woocommerce' ),
268268
),
269-
'visibility' => array(
269+
'visibility' => array(
270270
'type' => 'CatalogVisibilityEnum',
271271
'description' => __( 'Limit result set to products with a specific visibility level.', 'wp-graphql-woocommerce' ),
272272
),

includes/data/connection/class-product-connection-resolver.php

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -328,79 +328,74 @@ public function sanitize_input_fields( array $where_args ) {
328328

329329
$tax_query = array();
330330
$taxonomy_args = array(
331-
'type' => 'product_type',
332-
'typeIn' => 'product_type',
333-
'typeNotIn' => 'product_type',
334-
'categoryName' => 'product_cat',
335-
'category' => 'product_cat',
336-
'categoryIn' => 'product_cat',
337-
'categoryNotIn' => 'product_cat',
338-
'tagSlug' => 'product_tag',
339-
'tagId' => 'product_tag',
340-
'tagIn' => 'product_tag',
341-
'tagNotIn' => 'product_tag',
331+
'type' => 'product_type',
332+
'typeIn' => 'product_type',
333+
'typeNotIn' => 'product_type',
334+
'category' => 'product_cat',
335+
'categoryIn' => 'product_cat',
336+
'categoryNotIn' => 'product_cat',
337+
'categoryId' => 'product_cat',
338+
'categoryIdIn' => 'product_cat',
339+
'categoryIdNotIn' => 'product_cat',
340+
'tag' => 'product_tag',
341+
'tagIn' => 'product_tag',
342+
'tagNotIn' => 'product_tag',
343+
'tagId' => 'product_tag',
344+
'tagIdIn' => 'product_tag',
345+
'tagIdNotIn' => 'product_tag',
342346
);
343347

344348
foreach ( $taxonomy_args as $field => $taxonomy ) {
345349
if ( ! empty( $where_args[ $field ] ) ) {
350+
// Set tax query operator.
351+
switch ( true ) {
352+
case \wc_graphql_ends_with( $field, 'NotIn' ):
353+
$operator = 'NOT IN';
354+
break;
355+
default:
356+
$operator = 'IN';
357+
break;
358+
}
359+
360+
// Set tax query config.
346361
switch ( $field ) {
347362
case 'type':
348363
case 'typeIn':
349364
case 'typeNotIn':
350-
$tax_query[] = array(
351-
'taxonomy' => $taxonomy,
352-
'field' => 'slug',
353-
'terms' => $where_args[ $field ],
354-
);
355-
break;
356-
case 'categoryName':
357-
case 'categoryNameIn':
358-
case 'categoryNameNotIn':
359-
$tax_query[] = array(
360-
'taxonomy' => $taxonomy,
361-
'field' => 'slug',
362-
'terms' => $where_args[ $field ],
363-
);
364-
break;
365365
case 'category':
366366
case 'categoryIn':
367367
case 'categoryNotIn':
368-
$tax_query[] = array(
369-
'taxonomy' => $taxonomy,
370-
'field' => 'term_id',
371-
'terms' => $where_args[ $field ],
372-
);
373-
break;
374368
case 'tag':
375-
case 'tagSlugIn':
376-
case 'tagSlugNotIn':
369+
case 'tagIn':
370+
case 'tagNotIn':
377371
$tax_query[] = array(
378372
'taxonomy' => $taxonomy,
379373
'field' => 'slug',
380374
'terms' => $where_args[ $field ],
375+
'operator' => $operator,
381376
);
382377
break;
378+
case 'categoryId':
379+
case 'categoryIdIn':
380+
case 'categoryIdNotIn':
383381
case 'tagId':
384-
case 'tagIn':
385-
case 'tagNotIn':
382+
case 'tagIdIn':
383+
case 'tagIdNotIn':
386384
$tax_query[] = array(
387385
'taxonomy' => $taxonomy,
388386
'field' => 'term_id',
389387
'terms' => $where_args[ $field ],
388+
'operator' => $operator,
390389
);
391390
break;
392391
}
393-
394-
if ( \wc_graphql_ends_with( $field, 'NotIn' ) ) {
395-
$key = max( array_keys( $tax_query ) );
396-
$tax_query[ $key ]['operator'] = 'NOT IN';
397-
} elseif ( \wc_graphql_ends_with( $field, 'In' ) ) {
398-
$key = max( array_keys( $tax_query ) );
399-
$tax_query[ $key ]['operator'] = 'IN';
400-
}
401392
}
402393
}
403394

395+
if ( 1 < count( $tax_query ) ) {
396+
$tax_query['relation'] = 'AND';
397+
}
398+
404399
// Filter by attribute and term.
405400
if ( ! empty( $where_args['attribute'] ) && ! empty( $where_args['attributeTerm'] ) ) {
406401
if ( in_array( $where_args['attribute'], \wc_get_attribute_taxonomy_names(), true ) ) {

0 commit comments

Comments
 (0)