Files

131 lines
4.1 KiB
PHP
Raw Permalink Normal View History

2019-03-25 19:55:22 -04:00
<?php
/**
* Connection type - WC taxonomies.
2019-03-25 19:55:22 -04:00
*
* Registers connections to WC taxonomy types.
2019-03-25 19:55:22 -04:00
*
2019-10-25 19:13:36 -04:00
* @package WPGraphQL\WooCommerce\Connection
2019-03-25 19:55:22 -04:00
* @since 0.0.1
*/
2019-10-25 19:13:36 -04:00
namespace WPGraphQL\WooCommerce\Connection;
2019-03-25 19:55:22 -04:00
2019-12-18 15:11:59 -05:00
use GraphQL\Error\UserError;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Data\Connection\TermObjectConnectionResolver;
use WPGraphQL\Type\Connection\TermObjects;
2019-03-25 19:55:22 -04:00
/**
* Class - WC_Terms
*/
2019-03-27 11:36:31 -04:00
class WC_Terms extends TermObjects {
/**
2020-02-27 21:30:21 -03:00
* Registers the various connections from other Types to WooCommerce taxonomies.
2023-06-13 23:17:02 +03:00
*
* @throws \Exception If the "product_cat" taxonomy is not found.
*
* @return void
2019-03-27 11:36:31 -04:00
*/
2019-03-25 19:55:22 -04:00
public static function register_connections() {
2020-08-06 16:02:46 -04:00
// From Coupons to ProductCategory connections.
$tax_object = get_taxonomy( 'product_cat' );
2023-06-13 23:17:02 +03:00
if ( ! $tax_object ) {
throw new \Exception( __( '"product_cat" taxonomy not found', 'graphql-for-ecommerce' ) );
2023-06-13 23:17:02 +03:00
}
2019-03-25 19:55:22 -04:00
register_graphql_connection(
self::get_connection_config(
2020-08-06 16:02:46 -04:00
$tax_object,
[
2019-03-25 19:55:22 -04:00
'fromType' => 'Coupon',
'fromFieldName' => 'productCategories',
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $tax_object ) {
2021-01-21 22:41:43 -05:00
$resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $tax_object->name );
$term_taxonomy_ids = self::get_term_taxonomy_ids( $source->product_category_ids, $tax_object->name );
$resolver->set_query_arg( 'term_taxonomy_id', $term_taxonomy_ids );
2020-08-06 16:02:46 -04:00
return $resolver->get_connection();
2021-01-21 22:41:43 -05:00
},
]
2019-03-25 19:55:22 -04:00
)
);
register_graphql_connection(
self::get_connection_config(
2020-08-06 16:02:46 -04:00
$tax_object,
[
2019-03-25 19:55:22 -04:00
'fromType' => 'Coupon',
'fromFieldName' => 'excludedProductCategories',
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $tax_object ) {
$resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $tax_object->name );
$term_taxonomy_ids = self::get_term_taxonomy_ids( $source->excluded_product_category_ids, $tax_object->name );
$resolver->set_query_arg( 'term_taxonomy_id', $term_taxonomy_ids );
2020-08-06 16:02:46 -04:00
return $resolver->get_connection();
},
]
2019-03-25 19:55:22 -04:00
)
);
2019-12-18 15:11:59 -05:00
register_graphql_connection(
[
2019-12-18 15:11:59 -05:00
'fromType' => 'GlobalProductAttribute',
'toType' => 'TermNode',
'queryClass' => 'WP_Term_Query',
'fromFieldName' => 'terms',
'connectionArgs' => self::get_connection_args(
[
'orderby' => [
'type' => 'ProductAttributesConnectionOrderbyEnum',
'description' => static function () {
return __( 'Field(s) to order terms by. Defaults to \'name\'.', 'graphql-for-ecommerce' );
},
],
]
),
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
2019-12-18 15:11:59 -05:00
if ( ! $source->is_taxonomy() ) {
throw new UserError( __( 'Invalid product attribute', 'graphql-for-ecommerce' ) );
2019-12-18 15:11:59 -05:00
}
2020-02-27 21:30:21 -03:00
2020-08-06 16:02:46 -04:00
$resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $source->get_name() );
$resolver->set_query_arg( 'slug', $source->get_slugs() );
2020-02-27 21:30:21 -03:00
return $resolver->get_connection();
2019-12-18 15:11:59 -05:00
},
]
2019-12-18 15:11:59 -05:00
);
2019-03-25 19:55:22 -04:00
}
/**
* Converts an array of term_ids to their corresponding term_taxonomy_ids
* for a given taxonomy.
*
* WooCommerce stores term_ids on coupons, but WP_Term_Query's
* term_taxonomy_id arg requires term_taxonomy_ids. These can differ
* when terms are shared across taxonomies.
*
* @param array<int|string> $term_ids Term IDs to convert.
* @param string $taxonomy Taxonomy name.
*
* @return array<int> Term taxonomy IDs.
*/
private static function get_term_taxonomy_ids( array $term_ids, string $taxonomy ): array {
if ( empty( $term_ids ) || [ '0' ] === $term_ids || [ 0 ] === $term_ids ) {
return [ 0 ];
}
$terms = get_terms(
[
'taxonomy' => $taxonomy,
'include' => array_map( 'absint', $term_ids ),
'fields' => 'tt_ids',
'hide_empty' => false,
]
);
return ! empty( $terms ) && ! is_wp_error( $terms ) ? array_map( 'intval', $terms ) : [ 0 ];
}
2019-03-27 11:36:31 -04:00
}