From 2c5d3d2acc38395ca9be17b8c18c0f21ca9542d3 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Mon, 31 Oct 2022 14:26:28 +0530 Subject: [PATCH] Check order type is set before returning to prevent notice. (#35349) * Check order type is set before returning to prevent notice. * Applied code standards. * Remove type declaration since its not consistent with CPT datastore. * Switch to a yoda condition (satisfy required linting check). Co-authored-by: barryhughes <3594411+barryhughes@users.noreply.github.com> --- plugins/woocommerce/changelog/fix-order_type | 4 ++++ .../DataStores/Orders/OrdersTableDataStore.php | 2 +- .../src/Internal/Utilities/COTMigrationUtil.php | 4 ++-- .../Orders/OrdersTableDataStoreTests.php | 17 +++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-order_type diff --git a/plugins/woocommerce/changelog/fix-order_type b/plugins/woocommerce/changelog/fix-order_type new file mode 100644 index 00000000000..ba0073e6737 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-order_type @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Check order type is set before returning to prevent notice. diff --git a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php index 826cc0ce5c6..64885983ab5 100644 --- a/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php +++ b/plugins/woocommerce/src/Internal/DataStores/Orders/OrdersTableDataStore.php @@ -948,7 +948,7 @@ WHERE */ public function get_order_type( $order_id ) { $type = $this->get_orders_type( array( $order_id ) ); - return $type[ $order_id ]; + return $type[ $order_id ] ?? ''; } /** diff --git a/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php b/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php index 1c88e5074b5..d0a17ffeb47 100644 --- a/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php +++ b/plugins/woocommerce/src/Internal/Utilities/COTMigrationUtil.php @@ -75,7 +75,7 @@ class COTMigrationUtil { */ public function is_custom_order_tables_in_sync() : bool { $sync_status = $this->data_synchronizer->get_sync_status(); - return $sync_status['current_pending_count'] === 0 && $this->data_synchronizer->data_sync_is_enabled(); + return 0 === $sync_status['current_pending_count'] && $this->data_synchronizer->data_sync_is_enabled(); } /** @@ -160,7 +160,7 @@ class COTMigrationUtil { * * @return string|null Type of the order. */ - public function get_order_type( $order_id ) : ?string { + public function get_order_type( $order_id ) { $order_id = $this->get_post_or_order_id( $order_id ); $order_data_store = \WC_Data_Store::load( 'order' ); return $order_data_store->get_order_type( $order_id ); diff --git a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php index 37f4f850469..3b512c14328 100644 --- a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php +++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php @@ -1868,6 +1868,23 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case { $this->assertFalse( wc_get_order( $product->get_id() ) ); } + /** + * @testDox Make sure that getting order type for non order return without warning. + */ + public function test_get_order_type_for_non_order() { + $product = WC_Helper_Product::create_simple_product(); + $product->save(); + $this->assertEquals( '', $this->sut->get_order_type( $product->get_id() ) ); + } + + /** + * @testDox Test get order type working as expected. + */ + public function test_get_order_type_for_order() { + $order = $this->create_complex_cot_order(); + $this->assertEquals( 'shop_order', $this->sut->get_order_type( $order->get_id() ) ); + } + /** * @testDox Test that we are not duplicating address indexing when updating. */