Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::persist_order_to_dbprotectedWC 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 10.3.5

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

	if ( 'create' === $context ) {
		$post_id = $this->maybe_create_backup_post( $order, 'create' );
		if ( ! $post_id ) {
			throw new \Exception( esc_html__( '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->persist_db_row( $update );
		if ( false === $result ) {
			// translators: %s is a table name.
			throw new \Exception( esc_html( 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 );

	if ( $order->has_cogs() && $this->cogs_is_enabled() ) {
		$this->save_cogs_data( $order );
	}

	$this->clear_cached_data( array( $order->get_id() ) );
}