Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_db_row_from_order()protectedWC 6.8.0

Produces an array with keys 'row' and 'format' that can be passed to $wpdb->update() as the $data and $format parameters. Values are taken from the order changes array and properly formatted for inclusion in the database.

Method of the class: OrdersTableDataStore{}

No Hooks.

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_db_row_from_order( $order, $column_mapping, $only_changes );
$order(\WC_Abstract_Order) (required)
Order.
$column_mapping(array) (required)
Table column mapping.
$only_changes(true|false)
Whether to consider only changes in the order object or all fields.
Default: false

Changelog

Since 6.8.0 Introduced.

OrdersTableDataStore::get_db_row_from_order() code WC 8.7.0

protected function get_db_row_from_order( $order, $column_mapping, $only_changes = false ) {
	$changes = $only_changes ? $order->get_changes() : array_merge( $order->get_data(), $order->get_changes() );

	// Make sure 'status' is correctly prefixed.
	if ( array_key_exists( 'status', $column_mapping ) && array_key_exists( 'status', $changes ) ) {
		$changes['status'] = $this->get_post_status( $order );
	}

	$row        = array();
	$row_format = array();

	foreach ( $column_mapping as $column => $details ) {
		if ( ! isset( $details['name'] ) || ! array_key_exists( $details['name'], $changes ) ) {
			continue;
		}

		$row[ $column ]        = $this->database_util->format_object_value_for_db( $changes[ $details['name'] ], $details['type'] );
		$row_format[ $column ] = $this->database_util->get_wpdb_format_for_type( $details['type'] );
	}

	if ( ! $row ) {
		return false;
	}

	return array(
		'data'   => $row,
		'format' => $row_format,
	);
}