Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::persist_order_to_dbprotectedWC 6.8.0

Persists order changes to the database.

Method of the class: OrdersTableDataStore{}

No Hooks.

Returns

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->persist_order_to_db( $order, $force_all_fields );
$order(WC_Abstract_Order) (required) (passed by reference — &)
The order.
$force_all_fields(true|false)
Force saving all fields to DB and just changed.
Default: false

Changelog

Since 6.8.0 Introduced.

OrdersTableDataStore::persist_order_to_db() code WC 10.7.0

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, ! $only_changes || array_key_exists( 'cogs_total_value', $changes ) );
	}

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