When HPOS is enabled, wc_get_order() should still utilize global post and order objects (if needed).
This commit is contained in:
parent
38822cb3e9
commit
764f8d4490
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: fix
|
||||
|
||||
Ensure wc_get_order() works without arguments when HPOS is enabled.
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use Automattic\WooCommerce\Utilities\OrderUtil;
|
||||
|
||||
/**
|
||||
* Order factory class
|
||||
*/
|
||||
|
@ -165,10 +167,8 @@ class WC_Order_Factory {
|
|||
* @return int|bool false on failure
|
||||
*/
|
||||
public static function get_order_id( $order ) {
|
||||
global $post;
|
||||
|
||||
if ( false === $order && is_a( $post, 'WP_Post' ) && 'shop_order' === get_post_type( $post ) ) {
|
||||
return absint( $post->ID );
|
||||
if ( false === $order ) {
|
||||
return self::get_global_order_id();
|
||||
} elseif ( is_numeric( $order ) ) {
|
||||
return $order;
|
||||
} elseif ( $order instanceof WC_Abstract_Order ) {
|
||||
|
@ -180,6 +180,33 @@ class WC_Order_Factory {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to determine the current order ID based on available global state.
|
||||
*
|
||||
* @return false|int
|
||||
*/
|
||||
private static function get_global_order_id() {
|
||||
global $post;
|
||||
global $theorder;
|
||||
|
||||
// Initialize the global $theorder object if necessary.
|
||||
if ( ! isset( $theorder ) || ! $theorder instanceof WC_Abstract_Order ) {
|
||||
if ( ! isset( $post ) || 'shop_order' !== $post->post_type ) {
|
||||
return false;
|
||||
} else {
|
||||
OrderUtil::init_theorder_object( $post );
|
||||
}
|
||||
}
|
||||
|
||||
if ( $theorder instanceof WC_Order ) {
|
||||
return $theorder->get_id();
|
||||
} elseif ( is_a( $post, 'WP_Post' ) && 'shop_order' === get_post_type( $post ) ) {
|
||||
return absint( $post->ID );
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the class name bunch of order instances should have based on their IDs.
|
||||
*
|
||||
|
|
|
@ -162,6 +162,11 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case {
|
|||
* @group test
|
||||
*/
|
||||
public function test_wc_get_order() {
|
||||
global $post;
|
||||
global $theorder;
|
||||
|
||||
$original_post = $post;
|
||||
$original_theorder = $theorder;
|
||||
|
||||
$order = WC_Helper_Order::create_order();
|
||||
|
||||
|
@ -178,11 +183,29 @@ class WC_Tests_Order_Functions extends WC_Unit_Test_Case {
|
|||
$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 a random (incorrect) id.
|
||||
$this->assertFalse( wc_get_order( 123456 ) );
|
||||
|
||||
// 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 ) );
|
||||
$post = get_post( $order->get_id() );
|
||||
$this->assertInstanceOf(
|
||||
'WC_Order',
|
||||
wc_get_order(),
|
||||
'If no order ID is specified, wc_get_order() will use the global $post object to try and determine the current order.'
|
||||
);
|
||||
|
||||
unset( $post );
|
||||
$theorder = $order;
|
||||
$this->assertInstanceOf(
|
||||
'WC_Order',
|
||||
wc_get_order(),
|
||||
'If no order ID is specified, wc_get_order() will use the global $theorder object to try and determine the current order.'
|
||||
);
|
||||
|
||||
$post = $original_post;
|
||||
$theorder = $original_theorder;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue