Answer the question
In order to leave comments, you need to log in
How to automatically change the order status when the delivery status changes?
Can you help me figure out how to make an automated change in the status of an order when changing the delivery status when editing an order? For example, we set the delivery status to "Submitted to [DS]" delivery service, and at the same time, the order status automatically changes to "Sent [O]"?
Answer the question
In order to leave comments, you need to log in
$bxEventManager = \Bitrix\Main\EventManager::getInstance();
$bxEventManager->addEventHandler(
'sale',
'OnSaleShipmentEntitySaved',
array(
"\\Gricuk\\Shipment",
"onSaleShipmentSavedHandler"
)
);
<?php
namespace Gricuk;
use Bitrix\Main\Event;
use Bitrix\Main\EventResult;
use Bitrix\Main\Result;
class Shipment
{
public static function onSaleShipmentSavedHandler(\Bitrix\Main\Event $event)
{
/** @var \Bitrix\Sale\Shipment $shipment */
$shipment = $event->getParameter("ENTITY");
$oldValues = $event->getParameter("VALUES");
$shipmentFields = $shipment->getFieldValues();
/**
* Ключ - статус ДОСТАВКИ
* Значение - статус ЗАКАЗА
*/
$statusMap = [
"DR" => "F",
"DB" => "RS"
];
if (isset($statusMap[$shipmentFields["STATUS_ID"]])) {
try {
$orderId = $shipment->getParentOrderId();
$order = \Bitrix\Sale\Order::load($orderId);
$order->setField("STATUS_ID", $statusMap[$shipmentFields["STATUS_ID"]]);
/** @var Result $saveResult */
$saveResult = $order->save();
if (!$saveResult->isSuccess()) {
\Bitrix\Main\Diag\Debug::dumpToFile($saveResult, '$saveResult', 'onSaleShipmentSavedHandler.log');
}
} catch (\Exception $e) {
\Bitrix\Main\Diag\Debug::dumpToFile($e, '$e', 'onSaleShipmentSavedHandler.log');
}
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question