diff --git a/src/Integration/WPCOMProxy.php b/src/Integration/WPCOMProxy.php index 5109bbd4b7..e20b1088ab 100644 --- a/src/Integration/WPCOMProxy.php +++ b/src/Integration/WPCOMProxy.php @@ -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 ); @@ -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 ) { diff --git a/tests/Unit/Integration/WPCOMProxyTest.php b/tests/Unit/Integration/WPCOMProxyTest.php index 8af32bf0c2..403aa18ed5 100644 --- a/tests/Unit/Integration/WPCOMProxyTest.php +++ b/tests/Unit/Integration/WPCOMProxyTest.php @@ -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; + } }