From 8848e4aa47e0561ef98a37f2dc789fd0e726b6b3 Mon Sep 17 00:00:00 2001 From: Vedanshu Jain Date: Thu, 20 Oct 2022 14:47:58 +0530 Subject: [PATCH] Check before getting order classname to see if it exists. (#35207) --- plugins/woocommerce/changelog/fix-warning_order | 4 ++++ .../woocommerce/includes/class-wc-order-factory.php | 2 +- .../DataStores/Orders/OrdersTableDataStoreTests.php | 11 +++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce/changelog/fix-warning_order diff --git a/plugins/woocommerce/changelog/fix-warning_order b/plugins/woocommerce/changelog/fix-warning_order new file mode 100644 index 00000000000..f0f37f3dce1 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-warning_order @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Check whether order has classname before returning. diff --git a/plugins/woocommerce/includes/class-wc-order-factory.php b/plugins/woocommerce/includes/class-wc-order-factory.php index 3d023d6a7fb..bc096b0016d 100644 --- a/plugins/woocommerce/includes/class-wc-order-factory.php +++ b/plugins/woocommerce/includes/class-wc-order-factory.php @@ -233,7 +233,7 @@ class WC_Order_Factory { */ private static function get_class_name_for_order_id( $order_id ) { $classname = self::get_class_names_for_order_ids( array( $order_id ) ); - return $classname[ $order_id ]; + return $classname[ $order_id ] ?? false; } } 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 7625a57d01c..5e38ff264cb 100644 --- a/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php +++ b/plugins/woocommerce/tests/php/src/Internal/DataStores/Orders/OrdersTableDataStoreTests.php @@ -1824,4 +1824,15 @@ class OrdersTableDataStoreTests extends WC_Unit_Test_Case { $this->sut->read_multiple( $orders ); $this->assertFalse( $should_sync_callable->call( $this->sut, $order ) ); } + + /** + * @testDox Make sure get_order return false when checking an order of different order types without warning. + */ + public function test_get_order_with_id_for_different_type() { + $this->toggle_cot( true ); + $this->disable_cot_sync(); + $product = new \WC_Product(); + $product->save(); + $this->assertFalse( wc_get_order( $product->get_id() ) ); + } }