Skip to content

Commit

Permalink
Merge pull request #2455 from woocommerce/tweak/gla-attributes-respon…
Browse files Browse the repository at this point in the history
…se-empty-array

[API Pull] Ensure empty GLA attributes are returned as an object instead of an empty array in the responses.
  • Loading branch information
jorgemd24 authored Jul 8, 2024
2 parents 062099f + 0642055 commit bad2524
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Integration/WPCOMProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ public function prepare_response( WP_REST_Response $response, $item, WP_REST_Req
$resource = $this->get_route_pieces( $request )['resource'] ?? null;

if ( $item instanceof WC_Product && ( $resource === 'products' || $resource === 'variations' ) ) {
$attr = $this->attribute_manager->get_all_aggregated_values( $item );
$data['gla_attributes'] = $attr;
$attr = $this->attribute_manager->get_all_aggregated_values( $item );
// In case of empty array, convert to object to keep the response consistent.
$data['gla_attributes'] = (object) $attr;
}

foreach ( $data['meta_data'] ?? [] as $key => $meta ) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Unit/Integration/WPCOMProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function test_get_products() {
$this->assertEquals( $product_1->get_id(), $response->get_data()[0]['id'] );
$this->assertEquals( $expected_metadata, $this->format_metadata( $response->get_data()[0]['meta_data'] ) );
$this->assertArrayHasKey( 'gla_attributes', $response->get_data()[0] );
$this->assertEquals( 'object', gettype( $response->get_data()[0]['gla_attributes'] ) );
}

public function test_get_products_with_gla_syncable_false() {
Expand Down Expand Up @@ -167,6 +168,7 @@ public function test_get_products_without_gla_visibility_metadata() {
$this->assertEquals( $product_2->get_id(), $response->get_data()[0]['id'] );
$this->assertEquals( $expected_metadata, $this->format_metadata( $response->get_data()[0]['meta_data'] ) );
$this->assertArrayHasKey( 'gla_attributes', $response->get_data()[0] );
$this->assertEquals( 'object', gettype( $response->get_data()[0]['gla_attributes'] ) );
}

public function test_get_product_without_gla_visibility_metadata() {
Expand Down Expand Up @@ -248,6 +250,7 @@ public function test_get_variations() {
$this->assertArrayHasKey( $variation['variation_id'], $response_mapped );
$this->assertEquals( $expected_metadata, $this->format_metadata( $response_mapped[ $variation['variation_id'] ]['meta_data'] ) );
$this->assertArrayHasKey( 'gla_attributes', $response->get_data()[0] );
$this->assertEquals( 'object', gettype( $response->get_data()[0]['gla_attributes'] ) );
}
}

Expand Down Expand Up @@ -295,6 +298,7 @@ public function test_get_specific_variation_with_gla_syncable() {

$this->assertEquals( $expected_metadata, $this->format_metadata( $response->get_data()['meta_data'] ) );
$this->assertArrayHasKey( 'gla_attributes', $response->get_data() );
$this->assertEquals( 'object', gettype( $response->get_data()['gla_attributes'] ) );
}

public function test_get_specific_variation_without_gla_syncable() {
Expand Down

0 comments on commit bad2524

Please sign in to comment.