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.

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

Возвращает

Массив.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_db_rows_for_order( $order, $context, $only_changes ): array;
$order(\WC_Order) (обязательный)
The order.
$context(строка)
The context: 'create' or 'update'.
По умолчанию: 'create'
$only_changes(true|false)
Whether to consider only changes in the order for generating the rows.
По умолчанию: false

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

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

Код OrdersTableDataStore::get_db_rows_for_order() WC 6.9.1

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

	$existing_order_row = $order->get_id() ? $this->get_order_data_for_id( $order->get_id() ) : 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' ) ),
			'where'        => 'update' === $context ? array( 'id' => $order->get_id() ) : null,
			'where_format' => 'update' === $context ? '%d' : null,
		);
	}

	// 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' ) ),
			'where'        => isset( $existing_order_row->{"{$this->get_op_table_alias()}_id"} ) ? array( 'order_id' => $order->get_id() ) : null,
			'where_format' => isset( $existing_order_row->{"{$this->get_op_table_alias()}_id"} ) ? '%d' : null,
		);
	}

	// 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',
					)
				),
				'where'        => isset( $existing_order_row->{ $this->get_address_table_alias( $address_type ) . '_id' } )
								? array(
									'order_id'     => $order->get_id(),
									'address_type' => $address_type,
								)
								: null,
				'where_format' => isset( $existing_order_row->{ $this->get_address_table_alias( $address_type ) . '_id' } ) ? array( '%d', '%s' ) : null,
			);
		}
	}

	/**
	 * 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 );
}