Skip to content

Commit

Permalink
Merge pull request #2624 from woocommerce/fix/api-pull-type-prices
Browse files Browse the repository at this point in the history
[API PULL] Tweak WP Proxy Response to force the types of the price fields
  • Loading branch information
puntope authored Sep 27, 2024
2 parents 24c212e + 502a89e commit c93a26a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Integration/WPCOMProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ protected function register_object_types_filter( string $object_type ): void {
add_filter(
'woocommerce_rest_prepare_' . $object_type . '_object',
[ $this, 'filter_response_by_syncable_item' ],
1000, // Run this filter last to override any other response.
PHP_INT_MAX, // Run this filter last to override any other response.
3
);

add_filter(
'woocommerce_rest_prepare_' . $object_type . '_object',
[ $this, 'prepare_response' ],
10,
PHP_INT_MAX - 1,
3
);

Expand Down Expand Up @@ -338,6 +338,11 @@ public function prepare_response( WP_REST_Response $response, $item, WP_REST_Req
$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;

// Force types and prevent user type change for fields as Google has strict type requirements.
$data['price'] = strval( $data['price'] ?? null );
$data['regular_price'] = strval( $data['regular_price'] ?? null );
$data['sale_price'] = strval( $data['sale_price'] ?? null );
}

foreach ( $data['meta_data'] ?? [] as $key => $meta ) {
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/Integration/WPCOMProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,4 +498,49 @@ public function test_get_empty_settings_for_shipping_zone_methods_as_object() {
$proxy->prepare_data( $data, $request )
);
}

public function test_product_types() {
add_filter( 'woocommerce_rest_prepare_product_object', [ $this, 'alter_product_price_types' ], 10, 3 );

$product = ProductHelper::create_simple_product();
$product_variable = ProductHelper::create_variation_product();
$variation = $product_variable->get_available_variations()[0];
$this->add_metadata( $product->get_id(), $this->get_test_metadata() );

$request = $this->do_request( '/wc/v3/products', 'GET', [ 'gla_syncable' => '1' ] );
$this->assertEquals( 'string', gettype( $request->get_data()[0]['price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()[0]['regular_price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()[0]['sale_price'] ) );

$request = $this->do_request( '/wc/v3/products/' . $product->get_id(), 'GET', [ 'gla_syncable' => '1' ] );
$this->assertEquals( 'string', gettype( $request->get_data()['price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()['regular_price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()['sale_price'] ) );

$request = $this->do_request( '/wc/v3/products/' . $product_variable->get_id() . '/variations', 'GET', [ 'gla_syncable' => '1' ] );
$this->assertEquals( 'string', gettype( $request->get_data()[0]['price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()[0]['regular_price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()[0]['sale_price'] ) );

$request = $this->do_request( '/wc/v3/products/' . $product_variable->get_id() . '/variations/' . $variation['variation_id'], 'GET', [ 'gla_syncable' => '1' ] );
$this->assertEquals( 'string', gettype( $request->get_data()['price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()['regular_price'] ) );
$this->assertEquals( 'string', gettype( $request->get_data()['sale_price'] ) );

// Doesn't apply if here is not 'gla_syncable'
$request = $this->do_request( '/wc/v3/products' );
$this->assertEquals( 'integer', gettype( $request->get_data()[0]['price'] ) );
$this->assertEquals( 'integer', gettype( $request->get_data()[0]['regular_price'] ) );
$this->assertEquals( 'integer', gettype( $request->get_data()[0]['sale_price'] ) );

remove_filter( 'woocommerce_rest_prepare_product_object', [ $this, 'alter_product_price_types' ] );
}

public function alter_product_price_types( $response ) {
$response->data['price'] = intval( $response->data['price'] );
$response->data['regular_price'] = intval( $response->data['regular_price'] );
$response->data['sale_price'] = intval( $response->data['sale_price'] );

return $response;
}
}

0 comments on commit c93a26a

Please sign in to comment.