Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::persist_order_to_db()protectedWC 6.8.0

Persists order changes to the database.

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

Хуков нет.

Возвращает

null. Ничего (null).

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->persist_order_to_db( $order, $force_all_fields );
$order(\WC_Abstract_Order) (обязательный) (передается по ссылке — &)
The order.
$force_all_fields(true|false)
Force saving all fields to DB and just changed.
По умолчанию: false

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

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

Код OrdersTableDataStore::persist_order_to_db() WC 8.7.0

protected function persist_order_to_db( &$order, bool $force_all_fields = false ) {
	$context   = ( 0 === absint( $order->get_id() ) ) ? 'create' : 'update';
	$data_sync = wc_get_container()->get( DataSynchronizer::class );

	if ( 'create' === $context ) {
		$post_id = wp_insert_post(
			array(
				'post_type'     => $data_sync->data_sync_is_enabled() ? $order->get_type() : $data_sync::PLACEHOLDER_ORDER_POST_TYPE,
				'post_status'   => 'draft',
				'post_parent'   => $order->get_changes()['parent_id'] ?? $order->get_data()['parent_id'] ?? 0,
				'post_date'     => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getOffsetTimestamp() ),
				'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
			)
		);

		if ( ! $post_id ) {
			throw new \Exception( __( 'Could not create order in posts table.', 'woocommerce' ) );
		}

		$order->set_id( $post_id );
	}

	$only_changes = ! $force_all_fields && 'update' === $context;
	// Figure out what needs to be updated in the database.
	$db_updates = $this->get_db_rows_for_order( $order, $context, $only_changes );

	// Persist changes.
	foreach ( $db_updates as $update ) {
		// Make sure 'data' and 'format' entries match before passing to $wpdb.
		ksort( $update['data'] );
		ksort( $update['format'] );

		$result = $this->database_util->insert_on_duplicate_key_update(
			$update['table'],
			$update['data'],
			array_values( $update['format'] )
		);

		if ( false === $result ) {
			// translators: %s is a table name.
			throw new \Exception( sprintf( __( 'Could not persist order to database table "%s".', 'woocommerce' ), $update['table'] ) );
		}
	}

	$changes = $order->get_changes();
	$this->update_address_index_meta( $order, $changes );
	$default_taxonomies = $this->init_default_taxonomies( $order, array() );
	$this->set_custom_taxonomies( $order, $default_taxonomies );
}