Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong taxonomy label case in product attributes #869

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/type/interface/class-product-attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function get_fields() {
'type' => 'String',
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $attribute ) {
return ! empty( $attribute->get_name() ) ? ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) ) : null;
return ! empty( $attribute->get_name() ) ? $attribute->get_name() : null;
},
],
'options' => [
Expand Down
2 changes: 1 addition & 1 deletion includes/type/object/class-product-attribute-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function register() {
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $attribute ) {
$taxonomy = get_taxonomy( $attribute->get_name() );
return $taxonomy ? ucwords( $taxonomy->labels->singular_name ) : null;
return $taxonomy ? $taxonomy->labels->singular_name : null;
},
],
'name' => [
Expand Down
4 changes: 1 addition & 3 deletions includes/type/object/class-variation-attribute-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public static function register() {
return null;
}

$slug = \wc_attribute_taxonomy_slug( $source['name'] );
$label = preg_replace( '/(-|_)/', ' ', $slug );
return ! empty( $label ) ? ucwords( $label ) : null;
return \wc_attribute_taxonomy_slug( $source['name'] );
},
],
'name' => [
Expand Down
67 changes: 65 additions & 2 deletions tests/wpunit/ProductAttributeQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public function expectedProductAttributeData( $product_id, $path ) {
$this->expectedField(
'label',
$attribute->is_taxonomy()
? ucwords( get_taxonomy( $attribute->get_name() )->labels->singular_name )
: ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) )
? get_taxonomy( $attribute->get_name() )->labels->singular_name
: $attribute->get_name()
),
$this->expectedField( 'options', $attribute->get_slugs() ),
$this->expectedField( 'position', $attribute->get_position() ),
Expand Down Expand Up @@ -170,4 +170,67 @@ static function ( $attribute ) {

$this->assertQuerySuccessful( $response, $expected );
}

public function testProductAttributeMatchesVariationAttributeCounterpart() {
$product_id = $this->factory->product->createVariable();
$variation_ids = $this->factory->product_variation->createSome( $product_id )['variations'];

$query = '
query attributeQuery( $id: ID! ) {
product( id: $id ) {
id
attributes {
nodes {
name
label
options
}
}
... on ProductWithVariations {
variations {
nodes {
id
attributes {
nodes {
name
label
value
}
}
}
}
}
}
}
';

$variables = [ 'id' => $this->toRelayId( 'post', $product_id ) ];
$response = $this->graphql( compact( 'query', 'variables' ) );

/**
* Assert that the product attributes match the variation attributes
* without modification to confirm variations can be identified by product attribute.
*/
$attributes = $this->lodashGet( $response, 'data.product.attributes.nodes', [] );
$variations = $this->lodashGet( $response, 'data.product.variations.nodes', [] );

foreach( $variations as $variation ) {
$variation_attributes = $this->lodashGet( $variation, 'attributes.nodes', [] );
foreach( $variation_attributes as $variation_attribute ) {
$attribute_name = $variation_attribute['name'];
$attribute = array_search( $attribute_name, array_column( $attributes, 'name' ) );
$this->assertNotFalse( $attribute, sprintf( 'Variation attribute not found in product attributes for %s', $attribute_name ) );
if ( "" === $variation_attribute['value'] ) {
continue;
}

$this->assertContains( $variation_attribute['value'], $attributes[ $attribute ]['options'] );
}
}

$this->assertQuerySuccessful(
$response,
[ $this->expectedField( 'product.id', $this->toRelayId( 'post', $product_id ) ) ]
);
}
}
Loading