public function CommerceOrderEntityController::save in Commerce Core 7
Saves an order.
When saving an order without an order ID, this function will create a new order at that time. For new orders, it will also determine and save the order number and then save the initial revision of the order. Subsequent orders that should be saved as new revisions should set $order->revision to TRUE and include a log string in $order->log.
Parameters
$order: The full order object to save.
$transaction: An optional transaction object.
Return value
SAVED_NEW or SAVED_UPDATED depending on the operation performed.
Overrides DrupalCommerceEntityController::save
File
- modules/
order/ includes/ commerce_order.controller.inc, line 57 - The controller for the order entity containing the CRUD operations.
Class
- CommerceOrderEntityController
- The controller class for orders contains methods for the order CRUD operations. The load method is inherited from the default controller.
Code
public function save($order, DatabaseTransaction $transaction = NULL) {
if (!isset($transaction)) {
$transaction = db_transaction();
$started_transaction = TRUE;
}
try {
global $user;
// Determine if we will be inserting a new order.
$order->is_new = empty($order->order_id);
// Set the timestamp fields.
if ($order->is_new) {
if (empty($order->created)) {
$order->created = REQUEST_TIME;
}
if (empty($order->changed)) {
$order->changed = REQUEST_TIME;
}
if (empty($order->revision_timestamp)) {
$order->revision_timestamp = REQUEST_TIME;
}
if (empty($order->hostname)) {
$order->hostname = ip_address();
}
}
else {
// Otherwise if the order is not new but comes from an entity_create()
// or similar function call that initializes the created timestamp, uid,
// and hostname values to empty strings, unset them to prevent
// destroying existing data in those properties on update.
if ($order->created === '') {
unset($order->created);
}
if ($order->uid === '') {
unset($order->uid);
}
if ($order->hostname === '') {
unset($order->hostname);
}
$time = time();
$order->changed = $time;
$order->revision_timestamp = $time;
}
$order->revision_hostname = ip_address();
$order->revision_uid = $user->uid;
// Recalculate the order total using the current line item data.
commerce_order_calculate_total($order);
if ($order->is_new || !empty($order->revision)) {
// When inserting either a new order or revision, $order->log must be set
// because {commerce_order_revision}.log is a text column and therefore
// cannot have a default value. However, it might not be set at this
// point, so we ensure that it is at least an empty string in that case.
if (!isset($order->log)) {
$order->log = '';
}
}
elseif (empty($order->log)) {
// If we are updating an existing order without adding a new revision,
// we need to make sure $order->log is unset whenever it is empty. As
// long as $order->log is unset, drupal_write_record() will not attempt
// to update the existing database column when re-saving the revision.
unset($order->log);
}
return parent::save($order, $transaction);
} catch (Exception $e) {
if (!empty($started_transaction)) {
$transaction
->rollback();
watchdog_exception($this->entityType, $e);
}
throw $e;
}
}