2019-03-25 19:55:22 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2019-12-20 13:31:06 -05:00
|
|
|
* Connection type - WC taxonomies.
|
2019-03-25 19:55:22 -04:00
|
|
|
*
|
2019-12-20 13:31:06 -05: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;
|
2023-07-19 23:01:50 +03:00
|
|
|
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 ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
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,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-03-25 19:55:22 -04:00
|
|
|
'fromType' => 'Coupon',
|
|
|
|
|
'fromFieldName' => 'productCategories',
|
2023-07-20 00:11:25 +03:00
|
|
|
'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 );
|
2026-03-23 21:55:22 -04:00
|
|
|
|
|
|
|
|
$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
|
|
|
},
|
2022-08-26 14:53:36 -04: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,
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-03-25 19:55:22 -04:00
|
|
|
'fromType' => 'Coupon',
|
|
|
|
|
'fromFieldName' => 'excludedProductCategories',
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) use ( $tax_object ) {
|
2022-08-26 14:53:36 -04:00
|
|
|
$resolver = new TermObjectConnectionResolver( $source, $args, $context, $info, $tax_object->name );
|
2026-03-23 21:55:22 -04:00
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-03-25 19:55:22 -04:00
|
|
|
)
|
|
|
|
|
);
|
2019-12-18 15:11:59 -05:00
|
|
|
|
|
|
|
|
register_graphql_connection(
|
2022-08-26 14:53:36 -04:00
|
|
|
[
|
2019-12-18 15:11:59 -05:00
|
|
|
'fromType' => 'GlobalProductAttribute',
|
|
|
|
|
'toType' => 'TermNode',
|
|
|
|
|
'queryClass' => 'WP_Term_Query',
|
|
|
|
|
'fromFieldName' => 'terms',
|
2024-07-30 00:15:49 +03:00
|
|
|
'connectionArgs' => self::get_connection_args(
|
|
|
|
|
[
|
|
|
|
|
'orderby' => [
|
|
|
|
|
'type' => 'ProductAttributesConnectionOrderbyEnum',
|
2026-03-30 23:37:51 -04:00
|
|
|
'description' => static function () {
|
2026-06-30 10:16:21 -04:00
|
|
|
return __( 'Field(s) to order terms by. Defaults to \'name\'.', 'graphql-for-ecommerce' );
|
2026-03-30 23:37:51 -04:00
|
|
|
},
|
2024-07-30 00:15:49 +03:00
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
),
|
2023-07-20 00:11:25 +03:00
|
|
|
'resolve' => static function ( $source, array $args, AppContext $context, ResolveInfo $info ) {
|
2019-12-18 15:11:59 -05:00
|
|
|
if ( ! $source->is_taxonomy() ) {
|
2026-06-30 10:16:21 -04:00
|
|
|
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
|
|
|
},
|
2022-08-26 14:53:36 -04:00
|
|
|
]
|
2019-12-18 15:11:59 -05:00
|
|
|
);
|
2019-03-25 19:55:22 -04:00
|
|
|
}
|
2026-03-23 21: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
|
|
|
}
|