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 issue with syncing shipping rates with more than two decimals #2573

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
5 changes: 4 additions & 1 deletion src/Shipping/ShippingRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ class ShippingRate implements JsonSerializable {
* @param float $rate The shipping cost in store currency.
*/
public function __construct( float $rate ) {
$this->rate = $rate;
// Google only accepts rates with two decimal places.
// We avoid using wc_format_decimal or number_format_i18n because these functions format numbers according to locale settings, which may include thousands separators and different decimal separators.
// At this stage, we want to ensure the number is formatted strictly as a float, with no thousands separators and a dot as the decimal separator.
$this->rate = (float) number_format( $rate, 2 );
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Shipping/ZoneMethodsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ public function test_returns_flat_rate_methods_with_decimals_and_dot() {
$this->assertEquals( 10.6, $shipping_rates[0]->get_rate() );
}

public function test_returns_flat_rate_methods_with_more_than_two_decimals() {
$flat_rate = $this->createMock( WC_Shipping_Flat_Rate::class );
$flat_rate->id = ZoneMethodsParser::METHOD_FLAT_RATE;
$flat_rate->expects( $this->any() )
->method( 'get_option' )
->willReturnMap(
[
[ 'cost', null, '10.6123' ],
]
);

$zone = $this->createMock( WC_Shipping_Zone::class );
$zone->expects( $this->any() )
->method( 'get_shipping_methods' )
->willReturn( [ $flat_rate ] );

$shipping_rates = $this->methods_parser->parse( $zone );
$this->assertCount( 1, $shipping_rates );
$this->assertEquals( 10.61, $shipping_rates[0]->get_rate() );
}

public function test_returns_flat_rate_methods_including_shipping_classes() {
// Return three sample shipping classes.
$light_class = new \stdClass();
Expand Down