Automattic\WooCommerce\Internal\DataStores\Orders

OrdersTableDataStore::get_db_rows_for_order()protectedWC 6.8.0

Generates an array of rows with all the details required to insert or update an order in the database.

Method of the class: OrdersTableDataStore{}

Return

Array.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_db_rows_for_order( $order, $context, $only_changes ): array;
$order(\WC_Abstract_Order) (required)
The order.
$context(string)
The context: 'create' or 'update'.
Default: 'create'
$only_changes(true|false)
Whether to consider only changes in the order for generating the rows.
Default: false

Changelog

Since 6.8.0 Introduced.

OrdersTableDataStore::get_db_rows_for_order() code WC 7.1.0

protected function get_db_rows_for_order( \WC_Abstract_Order $order, string $context = 'create', bool $only_changes = false ): array {
	$result = array();

	$row = $this->get_db_row_from_order( $order, $this->order_column_mapping, $only_changes );
	if ( 'create' === $context && ! $row ) {
		throw new \Exception( 'No data for new record.' ); // This shouldn't occur.
	}

	if ( $row ) {
		$result[] = array(
			'table'  => self::get_orders_table_name(),
			'data'   => array_merge( $row['data'], array( 'id' => $order->get_id() ) ),
			'format' => array_merge( $row['format'], array( 'id' => '%d' ) ),
		);
	}

	// wc_order_operational_data.
	$row = $this->get_db_row_from_order( $order, $this->operational_data_column_mapping, $only_changes );
	if ( $row ) {
		$result[] = array(
			'table'  => self::get_operational_data_table_name(),
			'data'   => array_merge( $row['data'], array( 'order_id' => $order->get_id() ) ),
			'format' => array_merge( $row['format'], array( 'order_id' => '%d' ) ),
		);
	}

	// wc_order_addresses.
	foreach ( array( 'billing', 'shipping' ) as $address_type ) {
		$row = $this->get_db_row_from_order( $order, $this->{$address_type . '_address_column_mapping'}, $only_changes );

		if ( $row ) {
			$result[] = array(
				'table'  => self::get_addresses_table_name(),
				'data'   => array_merge(
					$row['data'],
					array(
						'order_id'     => $order->get_id(),
						'address_type' => $address_type,
					)
				),
				'format' => array_merge(
					$row['format'],
					array(
						'order_id'     => '%d',
						'address_type' => '%s',
					)
				),
			);
		}
	}

	/**
	 * Allow third parties to include rows that need to be inserted/updated in custom tables when persisting an order.
	 *
	 * @since 6.8.0
	 *
	 * @param array      Array of rows to be inserted/updated when persisting an order. Each entry should be an array with
	 *                   keys 'table', 'data' (the row), 'format' (row format), 'where' and 'where_format'.
	 * @param \WC_Order  The order object.
	 * @param string     The context of the operation: 'create' or 'update'.
	 */
	$ext_rows = apply_filters( 'woocommerce_orders_table_datastore_extra_db_rows_for_order', array(), $order, $context );

	return array_merge( $result, $ext_rows );
}