diff --git a/includes/class-wc-order-factory.php b/includes/class-wc-order-factory.php index d12ce6a90ba..0492bc5a81c 100644 --- a/includes/class-wc-order-factory.php +++ b/includes/class-wc-order-factory.php @@ -25,6 +25,8 @@ class WC_Order_Factory { $the_order = $post; } elseif ( is_numeric( $the_order ) ) { $the_order = get_post( $the_order ); + } elseif ( $the_order instanceof WC_Order ) { + $the_order = get_post( $the_order->id ); } if ( ! $the_order || ! is_object( $the_order ) ) { diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 41d00277b16..35645fc28a8 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -101,6 +101,7 @@ class WC_Unit_Tests_Bootstrap { require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php' ); require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php' ); + require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-order.php' ); } /** diff --git a/tests/unit-tests/order/functions.php b/tests/unit-tests/order/functions.php index 9572b29afb0..556e016878a 100644 --- a/tests/unit-tests/order/functions.php +++ b/tests/unit-tests/order/functions.php @@ -68,4 +68,34 @@ class Functions extends \WC_Unit_Test_Case { update_option( 'woocommerce_ship_to_destination', $default ); } + + /** + * Test wc_get_order() + * + * @since 2.4.0 + * @group test + */ + public function test_wc_get_order() { + + $order = \WC_Helper_Order::create_order(); + + // Assert that $order is a WC_Order object + $this->assertInstanceOf( 'WC_Order', $order ); + + // Assert that wc_get_order() accepts a WC_Order object + $this->assertInstanceOf( 'WC_Order', wc_get_order( $order ) ); + + // Assert that wc_get_order() accepts a order post id. + $this->assertInstanceOf( 'WC_Order', wc_get_order( $order->id ) ); + + // Assert that a non-shop_order post returns false + $post = $this->factory->post->create_and_get( array( 'post_type' => 'post' ) ); + $this->assertFalse( wc_get_order( $post->ID ) ); + + // Assert the return when $the_order args is false + $this->assertFalse( wc_get_order( false ) ); + + // Assert the return when $the_order args is a random (incorrect) id. + $this->assertFalse( wc_get_order( 123456 ) ); + } }