Automattic\WooCommerce\Internal\DataStores\Orders
OrdersTableDataStore::get_db_rows_for_order
Generates an array of rows with all the details required to insert or update an order in the database.
Method of the class: OrdersTableDataStore{}
Hooks from the method
Returns
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() OrdersTableDataStore::get db rows for order code WC 10.7.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(),
'type' => $order->get_type(),
)
),
'format' => array_merge(
$row['format'],
array(
'id' => '%d',
'type' => '%s',
)
),
);
}
// 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',
)
),
'where' => array(
'order_id' => $order->get_id(),
'address_type' => $address_type,
),
'where_format' => array( '%d', '%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 );
/**
* Filters the rows that are going to be inserted or updated during an order save.
*
* @since 8.8.0
* @internal Use 'woocommerce_orders_table_datastore_extra_db_rows_for_order' for adding rows to the database save.
*
* @param array $rows Array of rows to be inserted/updated. See 'woocommerce_orders_table_datastore_extra_db_rows_for_order' for exact format.
* @param \WC_Order $order The order object.
* @param string $context The context of the operation: 'create' or 'update'.
*/
$result = apply_filters(
'woocommerce_orders_table_datastore_db_rows_for_order',
array_merge( $result, $ext_rows ),
$order,
$context
);
return $result;
}