Skip to content

Commit f76ca6f

Browse files
committed
Add image support for productBrand query
1 parent 2ce9424 commit f76ca6f

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

includes/class-type-registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ public function init() {
137137
/**
138138
* Object fields.
139139
*/
140+
Type\WPObject\Product_Brand_Type::register_fields();
140141
Type\WPObject\Product_Category_Type::register_fields();
141142
Type\WPObject\Root_Query::register_fields();
142143

includes/class-wp-graphql-woocommerce.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ private function includes() {
290290
require $include_directory_path . 'type/object/class-product-attribute-object-type.php';
291291
require $include_directory_path . 'type/object/class-product-attribute-term-object-type.php';
292292
require $include_directory_path . 'type/object/class-product-attribute-types.php';
293+
require $include_directory_path . 'type/object/class-product-brand-type.php';
293294
require $include_directory_path . 'type/object/class-product-category-type.php';
294295
require $include_directory_path . 'type/object/class-product-download-type.php';
295296
require $include_directory_path . 'type/object/class-product-types.php';
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* WPObjectType - ProductBrand
4+
*
5+
* Registers fields for the ProductBrand type
6+
*
7+
* @package WPGraphQL\WooCommerce\Type\WPObject
8+
* @since 1.0.3
9+
*/
10+
11+
namespace WPGraphQL\WooCommerce\Type\WPObject;
12+
13+
use WPGraphQL\AppContext;
14+
15+
/**
16+
* Class - Product_Brand_Type
17+
*/
18+
class Product_Brand_Type {
19+
/**
20+
* Registers fields to ProductBrand.
21+
*
22+
* @return void
23+
*/
24+
public static function register_fields() {
25+
register_graphql_fields(
26+
'ProductBrand',
27+
[
28+
'image' => [
29+
'type' => 'MediaItem',
30+
'description' => static function () {
31+
return __( 'Product brand image', 'wp-graphql-woocommerce' );
32+
},
33+
'resolve' => static function ( $source, array $args, AppContext $context ) {
34+
$thumbnail_id = get_term_meta( $source->term_id, 'thumbnail_id', true );
35+
return ! empty( $thumbnail_id )
36+
? $context->get_loader( 'post' )->load_deferred( $thumbnail_id )
37+
: null;
38+
},
39+
],
40+
]
41+
);
42+
}
43+
}

tests/wpunit/ProductBrandQueriesTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,58 @@ public function testSingleProductBrandBySlug() {
9696
$this->assertQuerySuccessful( $response, $expected );
9797
}
9898

99+
/**
100+
* Test that productBrand image resolves from WooCommerce term thumbnail meta.
101+
*/
102+
public function testProductBrandImage() {
103+
$brand_id = $this->createProductBrand( 'brand-with-image' );
104+
$image_id = $this->factory->post->create(
105+
[
106+
'post_author' => $this->shop_manager,
107+
'post_status' => 'publish',
108+
'post_title' => 'Brand Image',
109+
'post_type' => 'attachment',
110+
]
111+
);
112+
113+
update_term_meta( $brand_id, 'thumbnail_id', $image_id );
114+
115+
$query = '
116+
query ($id: ID!) {
117+
productBrand(id: $id, idType: SLUG) {
118+
databaseId
119+
image {
120+
id
121+
}
122+
}
123+
productBrands(first: 100) {
124+
nodes {
125+
slug
126+
image {
127+
id
128+
}
129+
}
130+
}
131+
}
132+
';
133+
134+
$variables = [ 'id' => 'brand-with-image' ];
135+
$response = $this->graphql( compact( 'query', 'variables' ) );
136+
$expected = [
137+
$this->expectedField( 'productBrand.databaseId', $brand_id ),
138+
$this->expectedField( 'productBrand.image.id', $this->toRelayId( 'post', $image_id ) ),
139+
$this->expectedNode(
140+
'productBrands.nodes',
141+
[
142+
$this->expectedField( 'slug', 'brand-with-image' ),
143+
$this->expectedField( 'image.id', $this->toRelayId( 'post', $image_id ) ),
144+
]
145+
),
146+
];
147+
148+
$this->assertQuerySuccessful( $response, $expected );
149+
}
150+
99151
/**
100152
* Test the connection from productBrand to products.
101153
*/

0 commit comments

Comments
 (0)