Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
updatebasketitemaction.php
1<?php
2
4
8
16{
17 public function run(int $id, array $fields)
18 {
19 $result = [];
20
21 $updateBasketItemResult = $this->updateBasketItem($id, $fields);
22 if (!$updateBasketItemResult->isSuccess())
23 {
24 $this->addErrors($updateBasketItemResult->getErrors());
25 return $result;
26 }
27
28 $updateBasketItemData = $updateBasketItemResult->getData();
30 $basketItem = $updateBasketItemData['basketItem'];
31 return Sale\Helpers\Controller\Action\Entity\Order::getOrderProductByBasketItem($basketItem);
32 }
33
34 public function updateBasketItem(int $id, array $fields): Sale\Result
35 {
36 $result = new Sale\Result();
37
38 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
39
41 $basketClass = $registry->getBasketClassName();
42
43 $basketIterator = $basketClass::getList([
44 'select' => ['ORDER_ID', 'FUSER_ID', 'LID'],
45 'filter' => [
46 '=ID' => $id
47 ]
48 ]);
49
50 $fields = $this->prepareBasketFields($fields);
51
52 if ($basketItemData = $basketIterator->fetch())
53 {
54 if (empty($basketItemData['ORDER_ID']))
55 {
56 $basket = $this->getBasket($basketItemData['FUSER_ID'], $basketItemData['LID']);
57 if ($basket && !$basket->isEmpty())
58 {
59 $basketItem = $basket->getItemByBasketCode($id);
60 foreach ($fields as $fieldName => $fieldValue)
61 {
62 $setFieldResult = $basketItem->setField($fieldName, $fieldValue);
63 if (!$setFieldResult->isSuccess())
64 {
66 foreach ($setFieldResult->getErrors() as $error)
67 {
68 $result->addError(
69 new Main\Error(
70 $error->getMessage(),
72 )
73 );
74 }
75 }
76 }
77
78 $checkQuantityResult = $this->checkQuantity($basket);
79 if (!$checkQuantityResult->isSuccess())
80 {
81 foreach ($checkQuantityResult->getErrors() as $error)
82 {
83 $result->addError(
84 new Main\Error(
85 $error->getMessage(),
87 )
88 );
89 }
90 }
91
92 if ($result->isSuccess())
93 {
94 $saveResult = $basket->save();
95 if ($saveResult->isSuccess())
96 {
97 $result->setData([
98 'basket' => $basket,
99 'basketItem' => $basketItem,
100 ]);
101 }
102 else
103 {
105 foreach ($saveResult->getErrors() as $error)
106 {
107 $result->addError(
108 new Main\Error(
109 $error->getMessage(),
111 )
112 );
113 }
114 }
115 }
116 }
117 else
118 {
119 $result->addError(
120 new Main\Error(
121 'basket item load error',
123 )
124 );
125 }
126 }
127 else
128 {
129 $result->addError(
130 new Main\Error(
131 'there is order with this basket item',
133 )
134 );
135 }
136 }
137 else
138 {
139 $result->addError(
140 new Main\Error(
141 'basket item with id '.$id.' is not exists',
143 )
144 );
145 }
146
147 return $result;
148 }
149
150 private function getBasket(int $fuserId, string $siteId): Sale\Basket
151 {
153 $basketStorage = Sale\Basket\Storage::getInstance($fuserId, $siteId);
154 return $basketStorage->getBasket();
155 }
156
157 private function checkQuantity(Sale\Basket $basket): Sale\Result
158 {
159 $result = new Sale\Result();
160
161 $actualQuantityList = $this->getActualQuantityList($basket);
162 Sale\BasketComponentHelper::correctQuantityRatio($basket);
163
164 $updatedQuantityList = $this->getActualQuantityList($basket);
165 foreach ($updatedQuantityList as $basketCode => $itemQuantity)
166 {
167 if (!isset($actualQuantityList[$basketCode]) || $itemQuantity !== $actualQuantityList[$basketCode])
168 {
169 $result->addError(new Main\Error('quantity is incorrect'));
170 }
171 }
172
173 return $result;
174 }
175
176 private function getActualQuantityList(Sale\Basket $basket): array
177 {
178 $quantityList = [];
179
180 if (!$basket->isEmpty())
181 {
183 foreach ($basket as $basketItem)
184 {
185 if ($basketItem->canBuy() && !$basketItem->isDelay())
186 {
187 $quantityList[$basketItem->getBasketCode()] = $basketItem->getQuantity();
188 }
189 }
190 }
191
192 return $quantityList;
193 }
194
195 private function prepareBasketFields(array $fields): array
196 {
197 $fields = $this->filterBasketFieldsOnUpdate($fields);
198
199 $fields['PRODUCT_PROVIDER_CLASS'] = Catalog\Product\Basket::getDefaultProviderName();
200
201 $settableFields = Sale\BasketItemBase::getSettableFields();
202 return array_filter(
203 $fields,
204 static function ($field) use ($settableFields) {
205 return in_array($field, $settableFields, true);
206 },
207 ARRAY_FILTER_USE_KEY
208 );
209 }
210
211 private function filterBasketFieldsOnUpdate(array $basketFields): array
212 {
213 return (new Sale\Rest\Entity\BasketItem())->internalizeFieldsUpdate($basketFields);
214 }
215}
addErrors(array $errors)
Definition action.php:213