Answer the question
In order to leave comments, you need to log in
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
Registering a Handler
$bxEventManager = \Bitrix\Main\EventManager::getInstance();
$bxEventManager->addEventHandler(
"sale",
"OnSaleOrderBeforeSaved",
array(
Gricuk\Sale\Order::class,
"changeStatusOnOrderCreate"
)
);
$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;
}
}
}
}
}
$newStatusId = 'F';
$order->setField('STATUS_ID', $newStatusId);
$order->save();
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 questionAsk a Question
731 491 924 answers to any question