Created
August 22, 2019 14:47
-
-
Save ranaaterning/25a93df1351859a2dec2e11b79160857 to your computer and use it in GitHub Desktop.
Implementation of a filter query containing AND and OR statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use WPGraphQL\Type\WPInputObjectType; | |
use WPGraphQL\Types; | |
include_once('types.php'); | |
add_filter( 'graphql_input_fields', function ( $fields, $type_name, $config ) { | |
if ( $type_name === 'RootQueryToProductConnectionWhereArgs' ) { | |
$fields['filterQuery'] = [ | |
'description' => 'A filter query', | |
'type' => WPGraphQL\Types::list_of( | |
new WPInputObjectType( [ | |
'name' => 'relation', | |
'fields' => function() { | |
return [ | |
'OR' => [ | |
'name' => 'OR', | |
'type' => WPGraphQL\Types::list_of( product_filter_type('OR') ) | |
], | |
'AND' => [ | |
'name' => 'AND', | |
'type' => WPGraphQL\Types::list_of(product_filter_type('AND')) | |
] | |
]; | |
} | |
] ) | |
), | |
]; | |
} | |
return $fields; | |
}, 10, 3 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include_once('types.php'); | |
include_once('args.php'); | |
// ADD FILTERQUERY ARGUMENT TO PRODUCT TYPE | |
add_filter( 'graphql_map_input_fields_to_product_query', function( $query_args, $where_args, $source ) { | |
if(isset($where_args['filterQuery']) && !empty($where_args['filterQuery'])) { | |
$tax_query = ['relation' => 'AND']; | |
foreach ($where_args['filterQuery'] as $filter) { | |
foreach ($filter as $relation => $filter_args) { | |
foreach ($filter_args as $filter_arg) { | |
array_push($tax_query, | |
array( | |
'relation' => $relation, | |
array( | |
'taxonomy' => $filter_arg['taxonomy'], | |
'field' => 'slug', | |
'terms' => $filter_arg['terms'], | |
'operator' => 'IN', | |
) | |
) | |
); | |
} | |
} | |
} | |
$query_args['tax_query'] = $tax_query; | |
} | |
return $query_args; | |
}, 10, 3); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use WPGraphQL\Type\WPEnumType; | |
use WPGraphQL\Type\WPInputObjectType; | |
use WPGraphQL\Types; | |
function product_taxonomy_enum() { | |
$taxonomy_args = array( | |
'TYPE' => 'product_type', | |
'CATEGORY' => 'product_cat', | |
'TAG' => 'product_tag', | |
'BRAND' => 'product_brand', | |
); | |
$values = []; | |
foreach ($taxonomy_args as $key => $taxonomy) { | |
$values[$key] = array( | |
'name' => $key, | |
'value' => $taxonomy | |
); | |
} | |
// Add attributes to enum values | |
$attributes = wc_get_attribute_taxonomy_names(); | |
foreach ($attributes as $attribute) { | |
$attr_key = strtoupper($attribute); | |
$values[$attr_key] = array( | |
'name' => $attr_key, | |
'value' => $attribute | |
); | |
} | |
return $values; | |
} | |
// The allowed format of the filter query | |
function product_filter_type($type_name) { | |
$filter_type = new WPInputObjectType( [ | |
'name' => $type_name . 'filterArray', | |
'fields' => function() use ( $type_name ) { | |
return [ | |
'taxonomy' => [ | |
'type' => new WPEnumType( [ | |
'name' => $type_name . 'taxonomy', | |
'description' => __( 'Which field to select taxonomy term by.', 'beleco-market' ), | |
'values' => product_taxonomy_enum() | |
] ), | |
], | |
'terms' => [ | |
'type' => WPGraphQL\Types::list_of( WPGraphQL\Types::string() ), | |
'description' => __( 'A list of term slugs', 'beleco-market' ), | |
], | |
]; | |
} | |
] ); | |
return $filter_type; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment