Skip to content

Commit

Permalink
Fix fatal error in Multi-Currency Compatibility (#4819)
Browse files Browse the repository at this point in the history
* Fix fatal error by removing type hinting and returning early if expected type is not found.

* Add changelog.
  • Loading branch information
jessepearson authored Oct 4, 2022
1 parent 15dbfb6 commit 5b0c660
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-4818-multi-currency-compatibility-fatal
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Fix fatal error with call to MultiCurrency/Compatibility::convert_order_prices method.
8 changes: 4 additions & 4 deletions includes/multi-currency/Compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ public function attach_order_modifier( $arg ) {
*
* @param WC_Order[]|WC_Order_Refund[] $results The results returned by the orders query.
*
* @return array
* @return array|object of WC_Order objects
*/
public function convert_order_prices( $results ): array {
public function convert_order_prices( $results ) {
$backtrace_calls = [
'Automattic\WooCommerce\Admin\Notes\NewSalesRecord::sum_sales_for_date',
'Automattic\WooCommerce\Admin\Notes\NewSalesRecord::possibly_add_note',
];

// If the call we're expecting isn't in the backtrace, then just do nothing and return the results.
if ( ! $this->utils->is_call_in_backtrace( $backtrace_calls ) ) {
// If the results are not an array, or if the call we're expecting isn't in the backtrace, then just do nothing and return the results.
if ( ! is_array( $results ) || ! $this->utils->is_call_in_backtrace( $backtrace_calls ) ) {
return $results;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/unit/multi-currency/test-class-compatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,15 @@ public function test_filter_woocommerce_order_query() {
$order = wc_get_order( $result->get_id() );
$this->assertEquals( 1000, $order->get_total() );
}

public function test_filter_woocommerce_order_query_with_object_not_array() {
$order = wc_create_order();
$order->set_total( 1000 );
$order->set_currency( 'GBP' );

// Turn the order array into an object to confirm the object is returned as is.
$expected = (object) [ $order ];

$this->assertEquals( $expected, $this->compatibility->convert_order_prices( $expected, [] ) );
}
}

0 comments on commit 5b0c660

Please sign in to comment.