Skip to content

Commit a728e81

Browse files
committed
More support provided for WPGraphQL ACF
1 parent 42f9a21 commit a728e81

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

includes/class-acf-schema-filters.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
namespace WPGraphQL\WooCommerce;
1010

11+
use WPGraphQL\WooCommerce\Model\Coupon;
12+
use WPGraphQL\WooCommerce\Model\Order;
13+
use WPGraphQL\WooCommerce\Model\Product;
14+
1115
/**
1216
* Class ACF_Schema_Filters
1317
*/
@@ -19,6 +23,7 @@ class ACF_Schema_Filters {
1923
public static function add_filters() {
2024
// Registers WooCommerce CPTs && taxonomies.
2125
add_filter( 'graphql_acf_get_root_id', array( __CLASS__, 'resolve_crud_root_id' ), 10, 2 );
26+
add_filter( 'graphql_acf_post_object_source', array( __CLASS__, 'resolve_post_object_source' ), 10, 2 );
2227
}
2328

2429
/**
@@ -38,4 +43,32 @@ public static function resolve_crud_root_id( $id, $root ) {
3843

3944
return $id;
4045
}
46+
47+
/**
48+
* Filters ACF "post_object" field type resolver to ensure that
49+
* the proper Type source is provided for WooCommerce CPTs.
50+
*
51+
* @param mixed|null $source source of the data being provided.
52+
* @param mixed|null $value Post ID.
53+
*
54+
* @return mixed|null
55+
*/
56+
public static function resolve_post_object_source( $source, $value ) {
57+
$post = get_post( $value );
58+
if ( $post instanceof \WP_Post ) {
59+
switch ( $post->post_type ) {
60+
case 'shop_coupon':
61+
$source = new Coupon( $post->ID );
62+
break;
63+
case 'shop_order':
64+
$source = new Order( $post->ID );
65+
break;
66+
case 'product':
67+
$source = new Product( $post->ID );
68+
break;
69+
}
70+
}
71+
72+
return $source;
73+
}
4174
}

includes/class-core-schema-filters.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,15 @@ function( $type ) {
302302
* @param \WPGraphQL\Type\WPUnionType $wp_union WPUnion object.
303303
*/
304304
public static function inject_union_type_resolver( $type, $value, $wp_union ) {
305-
if ( 'product' === $value->post_type ) {
306-
$node = new \WPGraphQL\WooCommerce\Model\Product( $value->ID );
307-
$new_type = Factory::resolve_node_type( '', $node );
308-
if ( $new_type ) {
309-
$type = $wp_union->type_registry->get_type( $new_type );
310-
}
305+
switch ( get_class( $value ) ) {
306+
case 'WPGraphQL\WooCommerce\Model\Product':
307+
case 'WPGraphQL\WooCommerce\Model\Coupon':
308+
case 'WPGraphQL\WooCommerce\Model\Order':
309+
$new_type = Factory::resolve_node_type( $type, $value );
310+
if ( $new_type ) {
311+
$type = $wp_union->type_registry->get_type( $new_type );
312+
}
313+
break;
311314
}
312315

313316
return $type;

wp-graphql-woocommerce.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
// Exit if accessed directly.
2222
defined( 'ABSPATH' ) || exit;
2323

24+
define( 'GRAPHQL_DEBUG', true );
25+
2426
/**
2527
* If the codeception remote coverage file exists, require it.
2628
*

0 commit comments

Comments
 (0)