D
D
daylight62019-05-28 16:22:34
1C-Bitrix
daylight6, 2019-05-28 16:22:34

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

1 answer(s)
R
Roman Gritsuk, 2019-05-28
@daylight6

$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 question

Ask a Question

731 491 924 answers to any question