/** * Delete order. * * @param int $id Order id. * @return Result * @throws Main\ArgumentNullException */ public static function delete($id) { $result = new Result(); if (!($order = Order::load($id))) { $result->addError(new ResultError(Loc::getMessage('SALE_ORDER_ENTITY_NOT_FOUND'), 'SALE_ORDER_ENTITY_NOT_FOUND')); return $result; } Notify::setNotifyDisable(true); /** @var Result $r */ $r = $order->setField('CANCELED', 'Y'); if (!$r->isSuccess()) { $result->addErrors($r->getErrors()); return $result; } /** @var Basket $basketCollection */ if ($basketCollection = $order->getBasket()) { /** @var BasketItem $basketItem */ foreach ($basketCollection as $basketItem) { $basketItem->delete(); } } /** @var ShipmentCollection $shipmentCollection */ if ($shipmentCollection = $order->getShipmentCollection()) { /** @var Shipment $shipment */ foreach ($shipmentCollection as $shipment) { $shipment->delete(); } } /** @var PaymentCollection $paymentCollection */ if ($paymentCollection = $order->getPaymentCollection()) { /** @var Payment $payment */ foreach ($paymentCollection as $payment) { $payment->delete(); } } /** @var PropertyValueCollection $propertyCollection */ if ($propertyCollection = $order->getPropertyCollection()) { /** @var PropertyValue $property */ foreach ($propertyCollection as $property) { $property->delete(); } } $event = new Main\Event('sale', EventActions::EVENT_ON_BEFORE_ORDER_DELETE, array('ENTITY' => $order)); $event->send(); foreach ($event->getResults() as $eventResult) { $return = null; if ($eventResult->getType() == Main\EventResult::ERROR) { continue; } if ($eventResult->getType() == Main\EventResult::SUCCESS) { $return = $eventResult->getParameters('return'); if ($return !== null) { return $result; } } } /** @var Result $r */ $r = $order->save(); if ($r->isSuccess()) { /** @var Entity\DeleteResult $r */ $r = Internals\OrderTable::delete($id); if ($r->isSuccess()) { OrderHistory::deleteByOrderId($id); } } else { $result->addErrors($r->getErrors()); } Notify::setNotifyDisable(false); $event = new Main\Event('sale', EventActions::EVENT_ON_ORDER_DELETED, array('ENTITY' => $order, 'VALUE' => (bool) $r->isSuccess())); $event->send(); return $result; }