Documentation

Order saving

Saving is always performed using the order's method save(). Not only the order is saved, but also all entities associated with it (payment and shipment) specifically in the same consistent state that resulted after calculation of objects:

/** \Bitrix\Sale\Order $order */
$order = \Bitrix\Sale\Order::load(18);
$order->setField("USER_DESCRIPTION", "Deliver to apartment");

$shipmentCollection = $order->getShipmentCollection();
/** \Bitrix\Sale\Shipment $shipment */
foreach ($shipmentCollection as $shipment)
{
    if (!$shipment->isSystem())
        $shipment->allowDelivery();
}

$order->save();

There are two saving events that can interrupt the order saving process.

EventDescription and parameters
OnSaleOrderBeforeSaved Triggered at the start of saving process.

Parameters
ENTITY Order object.
VALUES Order field values.
OnSaleOrderSaved Triggered at the end of saving process, when order and all associated entities are already saved.

Parameters
ENTITY Order object.
VALUES Order field values.
IS_NEW Contains one of two values: true - when the order is new, false - when it is not. Using this flag allows removing looping when calling order saving in the event.

Example

//example of event OnSaleOrderSaved use

use Bitrix\Main; 
Main\EventManager::getInstance()->addEventHandler(
    'sale',
    'OnSaleOrderSaved',
    'myFunction'
);

//handler receives the sum to be used for specific actions in the future:

function myFunction(Main\Event $event)
{
    /** @var Order $order */
    $order = $event->getParameter("ENTITY");
    $oldValues = $event->getParameter("VALUES");
    $isNew = $event->getParameter("IS_NEW");

    if ($isNew)
    {
        $sum = $order->getPrice();
        // . . . 
    }
}



© «Bitrix24», 2001-2024
Up