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

[API PULL] Tweak WP Proxy Response to force the types of the price fields #2624

Merged
merged 2 commits into from
Sep 27, 2024
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
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;
}
}
Loading