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: Product attributes label inconsistences + syntax errors fix #771

Merged
merged 3 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix: attribute "name" and "label" inconsistencies resolved.
  • Loading branch information
kidunot89 committed Jul 19, 2023
commit 7ced0803670a7ab6617bc2ba88829cf55e92f457
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 @@ -64,7 +64,7 @@ public static function get_fields() {
'label' => [
'type' => 'String',
'description' => __( 'Attribute label', 'wp-graphql-woocommerce' ),
'resolve' => function ( $attribute ) {
'resolve' => static function ( $attribute ) {
return ! empty( $attribute->get_name() ) ? ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) ) : null;
},
],
Expand Down
4 changes: 1 addition & 3 deletions includes/type/object/class-product-attribute-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ public static function register() {
'type' => 'String',
'description' => __( 'Product attribute name', 'wp-graphql-woocommerce' ),
'resolve' => static function ( $attribute ) {
$taxonomy = get_taxonomy( $attribute->get_name() );

return $taxonomy ? $taxonomy->labels->singular_name : null;
return $attribute->get_name();
},
],
'slug' => [
Expand Down
5 changes: 3 additions & 2 deletions includes/type/object/class-variation-attribute-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public static function register() {
return null;
}

$slug = \wc_attribute_taxonomy_slug( $source['name'] );
return ucwords( preg_replace( '/(-|_)/', ' ', $slug ) );
$slug = \wc_attribute_taxonomy_slug( $source['name'] );
$label = preg_replace( '/(-|_)/', ' ', $slug );
return ! empty( $label ) ? ucwords( $label ) : null;
},
],
'name' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/wpunit/ProductAttributeQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public function expectedProductAttributeData( $product_id, $path ) {
[
$this->expectedField( 'id', base64_encode( $attribute_name . ':' . $product_id . ':' . $attribute->get_name() ) ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
$this->expectedField( 'attributeId', $attribute->get_id() ),
$this->expectedField( 'name', str_replace( 'pa_', '', $attribute->get_name() ) ),
$this->expectedField( 'name', $attribute->get_name() ),
$this->expectedField(
'label',
$attribute->is_taxonomy()
? ucwords( get_taxonomy( $attribute->get_name() )->labels->singular_name )
: ucfirst( $attribute->get_name() )
: ucwords( preg_replace( '/(-|_)/', ' ', $attribute->get_name() ) )
),
$this->expectedField( 'options', $attribute->get_slugs() ),
$this->expectedField( 'position', $attribute->get_position() ),
Expand Down
2 changes: 1 addition & 1 deletion tests/wpunit/VariationAttributeQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function expectedDefaultAttributes( $id ) {

$expected = [];
foreach ( $attributes as $attribute ) {
$name = wc_attribute_label( $attribute->get_name() );
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$attribute_values = wc_get_product_terms( $id, $attribute->get_name(), [ 'fields' => 'all' ] );
foreach ( $attribute_values as $attribute_value ) {
Expand Down