Y
Y
Yuriy2021-05-12 11:12:12
1C-Bitrix
Yuriy, 2021-05-12 11:12:12

Bitrix order status substitution when order is changed?

how to replace the incoming status in an event and save another one?

\Bitrix\Main\EventManager::getInstance()->addEventHandler('sale', 'OnSaleStatusOrderChange', ['Handler', 'OnSaleStatusOrderChange']);

class Handler {
  function OnSaleStatusOrderChange(Bitrix\Main\Event $event)
  {

    $order = $event->getParameter("ENTITY");
    $value = $event->getParameter("VALUE");
    $oldValue = $event->getParameter("OLD_VALUE");

    $paymentCollection = $order->getPaymentCollection();
    $isPaid = $paymentCollection->isPaid();


    if ($value === 'PA' && $isPaid === true) {
     // ??? Меняем статус на нужный нам ???
     }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Roman Gritsuk, 2021-05-12
@yous

Registering a Handler

$bxEventManager = \Bitrix\Main\EventManager::getInstance();
$bxEventManager->addEventHandler(
    "sale",
    "OnSaleOrderBeforeSaved",
    array(
        Gricuk\Sale\Order::class,
        "changeStatusOnOrderCreate"
    )
);

The processor itself. In my example, I have logic to change the status of an order for certain services. It can be rewritten to anything. The main thing is$event->addResult
namespace Gricuk\Sale;

class Order
{
    public static function changeStatusOnOrderCreate(\Bitrix\Main\Event $event)
    {
        /** @var \Bitrix\Sale\Order $order */
        $order = $event->getParameter("ENTITY");

        $status = $order->getField("STATUS_ID");
        if ($status == "N") {
            /**  @var $paymentCollection \Bitrix\Sale\PaymentCollection */
            $paymentCollection = $order->getPaymentCollection();

            foreach ($paymentCollection as $payment) {
                /**  @var $payment \Bitrix\Sale\Payment */

                /**  @var $paySystem \Bitrix\Sale\PaySystem\Service */
                $paySystem = $payment->getPaySystem();

                if (!is_null($paySystem) && !$paySystem->isCash()) {
                    $order->setField("STATUS_ID", "NA");

                    $event->addResult(
                        new Main\EventResult(
                            Main\EventResult::SUCCESS, $order
                        )
                    );
                    break;
                }
            }
        }
    }
}

Example 100% working. Taken from sale.

P
PetrPo, 2021-05-12
@PetrPo

$newStatusId = 'F';
$order->setField('STATUS_ID', $newStatusId);
$order->save();

In the docks, there is an example Status Change

A
Andrey Nikolaev, 2021-05-12
@gromdron

You should not replace the status on the OnSaleStatusOrderChange event, since this event is called when the status changes. It is better to track the desired event before saving the order and change it already in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question