Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketitemupdateproductreservehandlers.php
1<?php
2
4
9
11{
12 private static $basketReserves = [];
13
21 public static function OnSaleBasketItemSetField(Event $event): void
22 {
23 $name = $event->getParameter('NAME');
24 $basketItem = $event->getParameter('ENTITY');
25 if ($name !== 'PRODUCT_ID' || !($basketItem instanceof BasketItem))
26 {
27 return;
28 }
29
30 $reserveQuantityCollection = $basketItem->getReserveQuantityCollection();
31 if (!isset($reserveQuantityCollection) || $reserveQuantityCollection->isEmpty())
32 {
33 return;
34 }
35
36 $reserves = [];
37 foreach ($reserveQuantityCollection as $reserveQuantity)
38 {
43 $reserves[] = [
44 'STORE_ID' => $reserveQuantity->getStoreId(),
45 'QUANTITY' => $reserveQuantity->getQuantity(),
46 'DATE_RESERVE_END' => $reserveQuantity->getField('DATE_RESERVE_END'),
47 ];
48 $result = $reserveQuantity->delete();
49
50 if (!$result->isSuccess())
51 {
52 $reserveQuantity->setFieldNoDemand('QUANTITY', 0);
53
54 Provider::tryReserve($reserveQuantity);
55
56 $reserveQuantity->deleteNoDemand();
57 }
58 }
59
60 self::$basketReserves[$basketItem->getBasketCode()] = $reserves;
61 }
62
70 public static function OnAfterSaleBasketItemSetField(Event $event): void
71 {
72 $name = $event->getParameter('NAME');
73 $basketItem = $event->getParameter('ENTITY');
74 if ($name !== 'PRODUCT_ID' || !($basketItem instanceof BasketItem))
75 {
76 return;
77 }
78
79 $reserveQuantityCollection = $basketItem->getReserveQuantityCollection();
80 if (!isset($reserveQuantityCollection))
81 {
82 return;
83 }
84
85 $reserves = self::$basketReserves[$basketItem->getBasketCode()] ?? null;
86 if (empty($reserves))
87 {
88 return;
89 }
90
91 foreach ($reserves as $reserve)
92 {
93 $reserveQuantity = $reserveQuantityCollection->create();
94 $result = $reserveQuantity->setFields($reserve);
95
96 if (!$result->isSuccess())
97 {
98 $reserveQuantity->setFieldsNoDemand($reserve);
99 Provider::tryReserve($reserveQuantity);
100 }
101 }
102
103 unset(self::$basketReserves[$basketItem->getBasketCode()]);
104 }
105}
getParameter($key)
Definition event.php:80