1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
basketproperties.php
См. документацию.
1<?php
2
3namespace Bitrix\Sale\Controller;
4
5use Bitrix\Main\Engine\AutoWire\ExactParameter;
6use Bitrix\Main\Engine\Response\DataType\Page;
7use Bitrix\Main\Error;
8use Bitrix\Main\UI\PageNavigation;
9use Bitrix\Sale;
10use Bitrix\Sale\BasketPropertiesCollection;
11use Bitrix\Sale\BasketPropertyItem;
12use Bitrix\Sale\Result;
13
15{
16 public function getPrimaryAutoWiredParameter()
17 {
18 return new ExactParameter(
19 BasketPropertyItem::class,
20 'basketProperty',
21 function($className, $id)
22 {
24
26 $basketPropertyClass = $registry->getBasketPropertyItemClassName();
27
28 $b = $basketPropertyClass::getList([
29 'select'=>[
30 'BASKET_ID',
31 ],
32 'filter'=>[
33 '=ID' => $id,
34 ],
35 ]);
36
37 if ($bRow = $b->fetch())
38 {
40 $basketClass = $registry->getBasketClassName();
41
42 $r = $basketClass::getList([
43 'select'=>[
44 'ORDER_ID',
45 ],
46 'filter'=>[
47 '=ID' => (int)$bRow['BASKET_ID'],
48 ],
49 ]);
50
51 if ($row = $r->fetch())
52 {
54 $orderClass = $registry->getOrderClassName();
55
56 $order = $orderClass::load($row['ORDER_ID']);
57 $basket = $order->getBasket()->getItemByBasketCode($bRow['BASKET_ID']);
58 if ($basket)
59 {
60 $property = $basket->getPropertyCollection()->getItemById($id);
61 if ($property)
62 {
63 return $property;
64 }
65 }
66 }
67 }
68
69 $this->addError(new Error('basket property is not exists', 200240400003));
70
71 return null;
72 }
73 );
74 }
75
76 //region Actions
77 public function getFieldsAction()
78 {
80
81 return [
82 'BASKET_PROPERTIES' => $entity->prepareFieldInfos($entity->getFields()),
83 ];
84 }
85
86 public function listAction(
87 PageNavigation $pageNavigation,
88 array $select = [],
89 array $filter = [],
90 array $order = [],
91 bool $__calculateTotalCount = true
92 ): Page
93 {
94 $select = empty($select) ? ['*'] : $select;
95 $order = empty($order) ? ['ID' => 'ASC'] : $order;
96
97 $iterator = Sale\Internals\BasketPropertyTable::getList([
98 'select' => $select,
99 'filter' => $filter,
100 'order' => $order,
101 'offset' => $pageNavigation->getOffset(),
102 'limit' => $pageNavigation->getLimit(),
103 'count_total' => $__calculateTotalCount,
104 ]);
105 $items = $iterator->fetchAll();
106 $totalCount = $__calculateTotalCount ? $iterator->getCount(): 0;
107 unset($iterator);
108
109 return new Page('BASKET_PROPERTIES', $items, $totalCount);
110 }
111
112 public function getAction(BasketPropertyItem $basketProperty)
113 {
114 return [
115 'BASKET_PROPERTY' => $this->get($basketProperty),
116 ];
117 }
118
119 public function addAction(array $fields)
120 {
121 if (!isset($fields['BASKET_ID']))
122 {
123 $this->addError(new Error('Basket item id is absent', 200240400004));
124
125 return null;
126 }
127
128 $basketId = (int)$fields['BASKET_ID'];
129 if ($basketId <= 0)
130 {
131 $this->addError(new Error('Basket item id is bad', 200240400005));
132
133 return null;
134 }
135 unset($fields['BASKET_ID']);
136
138
140 $basketClass = $registry->getBasketClassName();
141
142 $iterator = $basketClass::getList([
143 'select'=>[
144 'ORDER_ID',
145 ],
146 'filter'=>[
147 '=ID' => $basketId,
148 ],
149 ]);
150 $row = $iterator->fetch();
151 unset($iterator);
152
153 if (empty($row))
154 {
155 $this->addError(new Error('Basket item not exists', 200240400002));
156
157 return null;
158 }
159
161 $orderClass = $registry->getOrderClassName();
162
163 $order = $orderClass::load($row['ORDER_ID']);
164 $basketItem = $order->getBasket()->getItemByBasketCode($basketId);
165
166 if (!($basketItem instanceof Sale\BasketItem))
167 {
168 $this->addError(new Error('Basket item not exists', 200240400001));
169
170 return null;
171 }
172
174 $propertyCollection = $basketItem->getPropertyCollection();
175 $basketProperty = $propertyCollection->createItem();
176 $result = $basketProperty->setFields($fields);
177
178 if ($result->isSuccess() && !$result->hasWarnings())
179 {
180 $r = $this->save($basketProperty);
181 if (!$r->isSuccess())
182 {
183 $result->addErrors($r->getErrors());
184 }
185 }
186
187 if (!$result->isSuccess())
188 {
189 $this->addErrors($result->getErrors());
190
191 return null;
192 }
193 elseif ($result->hasWarnings())
194 {
195 $this->addErrors($result->getWarnings());
196
197 return null;
198 }
199 else
200 {
201 return [
202 'BASKET_PROPERTY' => $this->get($basketProperty),
203 ];
204 }
205 }
206
207 public function updateAction(BasketPropertyItem $basketProperty, array $fields)
208 {
209 $r = $basketProperty->setFields($fields);
210
211 if (!$r->isSuccess())
212 {
213 $this->addErrors($r->getErrors());
214
215 return null;
216 }
217
218 if ($r->hasWarnings())
219 {
220 $this->addErrors($r->getWarnings());
221
222 return null;
223 }
224
225 $r = $this->save($basketProperty);
226 if (!$r->isSuccess())
227 {
228 $this->addErrors($r->getErrors());
229
230 return null;
231 }
232
233 return [
234 'BASKET_PROPERTY' => $this->get($basketProperty),
235 ];
236 }
237
238 public function deleteAction(BasketPropertyItem $basketProperty)
239 {
240 $r = $basketProperty->delete();
241
242 if (!$r->isSuccess())
243 {
244 $this->addErrors($r->getErrors());
245
246 return null;
247 }
248
249 if ($r->hasWarnings())
250 {
251 $this->addErrors($r->getWarnings());
252
253 return null;
254 }
255
256 $r = $this->save($basketProperty);
257 if (!$r->isSuccess())
258 {
259 $this->addErrors($r->getErrors());
260
261 return null;
262 }
263
264 return true;
265 }
266 //endregion
267
268 protected function get(BasketPropertyItem $basketProperty, array $fields = [])
269 {
271 $properties = $basketProperty->getCollection();
272 $basketItem = $properties->getBasketItem();
274 $basket = $basketItem->getCollection();
276 $order = $basket->getOrder();
277
278 $basketItems = $this->toArray($order, $fields)['ORDER']['BASKET_ITEMS'];
279 foreach ($basketItems as $item)
280 {
281 foreach ($item['PROPERTIES'] as $property)
282 {
283 if ($property['ID'] == $basketProperty->getId())
284 {
285 return $property;
286 }
287 }
288 }
289
290 return [];
291 }
292
293 private function save(BasketPropertyItem $basketProperty): Result
294 {
295 $result = new Result();
297 $properties = $basketProperty->getCollection();
298 $basketItem = $properties->getBasketItem();
300 $basket = $basketItem->getCollection();
302 $order = $basket->getOrder();
303
304 $r = $order->save();
305 if (!$r->isSuccess())
306 {
307 $result->addErrors($r->getErrors());
308 }
309 elseif ($r->hasWarnings())
310 {
311 $result->addErrors($r->getWarnings());
312 }
313
314 return $result;
315 }
316}
static delete($id)
Определения entity.php:317
addError(Error $error)
Определения controller.php:1070
addErrors(array $errors)
Определения controller.php:1083
getPrimaryAutoWiredParameter()
Определения controller.php:334
Определения error.php:15
listAction(PageNavigation $pageNavigation, array $select=[], array $filter=[], array $order=[], bool $__calculateTotalCount=true)
Определения basketproperties.php:86
updateAction(BasketPropertyItem $basketProperty, array $fields)
Определения basketproperties.php:207
deleteAction(BasketPropertyItem $basketProperty)
Определения basketproperties.php:238
getAction(BasketPropertyItem $basketProperty)
Определения basketproperties.php:112
static getInstance($type)
Определения registry.php:183
const REGISTRY_TYPE_ORDER
Определения registry.php:16
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
$select
Определения iblock_catalog_list.php:194
$filter
Определения iblock_catalog_list.php:54
Определения aliases.php:54
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
$items
Определения template.php:224
$totalCount
Определения subscription_card_product.php:51
$iterator
Определения yandex_run.php:610
$fields
Определения yandex_run.php:501