Automattic\WooCommerce\Internal\DataStores\Orders

LegacyDataHandler::get_order_from_datastore()publicWC 8.6.0

Returns an order object as seen by either the HPOS or CPT datastores.

Метод класса: LegacyDataHandler{}

Хуков нет.

Возвращает

\WC_Order. Order instance.

Использование

$LegacyDataHandler = new LegacyDataHandler();
$LegacyDataHandler->get_order_from_datastore( $order_id, $data_store_id );
$order_id(int) (обязательный)
Order ID.
$data_store_id(строка)
Datastore to use. Should be either 'hpos' or 'posts'.
По умолчанию: 'hpos'

Список изменений

С версии 8.6.0 Введена.

Код LegacyDataHandler::get_order_from_datastore() WC 9.7.1

public function get_order_from_datastore( int $order_id, string $data_store_id = 'hpos' ) {
	$data_store = ( 'hpos' === $data_store_id ) ? $this->data_store : $this->data_store->get_cpt_data_store_instance();

	wp_cache_delete( \WC_Order::generate_meta_cache_key( $order_id, 'orders' ), 'orders' );

	// Prime caches if we can.
	if ( method_exists( $data_store, 'prime_caches_for_orders' ) ) {
		$data_store->prime_caches_for_orders( array( $order_id ), array() );
	}

	$order_type = wc_get_order_type( $data_store->get_order_type( $order_id ) );

	if ( ! $order_type ) {
		// translators: %d is an order ID.
		throw new \Exception( esc_html( sprintf( __( '%d is not an order or has an invalid order type.', 'woocommerce' ), $order_id ) ) );
	}

	$classname = $order_type['class_name'];
	$order     = new $classname();
	$order->set_id( $order_id );

	// Switch datastore if necessary.
	$update_data_store_func = function ( $data_store ) {
		// Each order object contains a reference to its data store, but this reference is itself
		// held inside of an instance of WC_Data_Store, so we create that first.
		$data_store_wrapper = \WC_Data_Store::load( 'order' );

		// Bind $data_store to our WC_Data_Store.
		( function ( $data_store ) {
			$this->current_class_name = get_class( $data_store );
			$this->instance           = $data_store;
		} )->call( $data_store_wrapper, $data_store );

		// Finally, update the $order object with our WC_Data_Store( $data_store ) instance.
		$this->data_store = $data_store_wrapper;
	};
	$update_data_store_func->call( $order, $data_store );

	// Read order (without triggering sync) -- we create our own callback instead of using `__return_false` to
	// prevent `remove_filter()` from removing it in cases where it was already hooked by 3rd party code.
	$prevent_sync_on_read = fn() => false;

	add_filter( 'woocommerce_hpos_enable_sync_on_read', $prevent_sync_on_read, 999 );
	try {
		$data_store->read( $order );
	} finally {
		remove_filter( 'woocommerce_hpos_enable_sync_on_read', $prevent_sync_on_read, 999 );
	}

	return $order;
}