T
T
TechNOIR2019-01-16 11:05:41
1C-Bitrix
TechNOIR, 2019-01-16 11:05:41

1C-Bitrix. How to make the status "Cancelled" when canceling an order instead of "Completed"?

Good afternoon.
When canceling an order in Bitrix, the status "Completed" is set to the order.
How to make the status "Canceled" when canceling an order instead of "Completed"? If I create valid status "Cancelled" in statuses with CN code
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Gritsuk, 2019-01-16
@TechNOIR

We need to write an event handler for the sale OnSaleOrderBeforeSaved module.
Here is an example handler

<?
namespace Gricuk\Sale;


class OrderEvents
{
  /**
     * 1) Смена статуса для отмененного заказа
     * 2) Комиссия за наложенный платёж
     * @param \Bitrix\Main\Event $event
     * @link https://dev.1c-bitrix.ru/api_d7/bitrix/sale/events/order_saved.php
     */
  public static function OnSaleOrderBeforeSavedHandler(\Bitrix\Main\Event $event)
  {
    /** @var \Bitrix\Sale\Order $order */
      $order = $event->getParameter("ENTITY");
      global $USER;
      if (!$order->isNew()) {

          $orderStatus = $order->getField("STATUS_ID");
          if ($order->isCanceled() && ($orderStatus != "CN")) {
              $order->setField("STATUS_ID", "CN");
              $event->addResult(new \Bitrix\Main\EventResult(
                  \Bitrix\Main\EventResult::SUCCESS,
                  array(
                      "RESULT" => $order,
                  )
              ));
          }

          return;
      }
  }
}

This is how you connect the event
<?
$bxEventManager = \Bitrix\Main\EventManager::getInstance();

$bxEventManager->addEventHandler(
    "sale",
    "OnSaleOrderBeforeSaved",
    array(
         \Gricuk\Sale\OrderEvents::class,
        "OnSaleOrderBeforeSavedHandler"
    )
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question