Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
initskucollectionfromparams.php
1<?php
2
4
10
11trait InitSkuCollectionFromParams
12{
25 protected function initFieldsSkuCollectionItems(SkuCollection $skuCollection, array $rows, bool $isServiceForm): void
26 {
27 if (empty($rows))
28 {
29 return;
30 }
31
32 foreach ($rows as $id => $row)
33 {
34 if (!is_array($row))
35 {
36 continue;
37 }
38
39 $isNew = (string)(int)$id !== (string)$id; // available exist: 564; new ids: oX03gdf, 000000001450000000
40 $sku =
41 $isNew
42 ? $skuCollection->create()
43 : $skuCollection->findById((int)$id)
44 ;
45 if (!($sku instanceof BaseSku))
46 {
47 continue;
48 }
49
50
51 $form = $isServiceForm ? new GridServiceForm($sku) : new GridVariationForm($sku);
52 $fields = $form->prepareFieldsValues($row);
53
54 $sku->setFields($fields);
55
56 if (isset($fields['PROPERTIES']))
57 {
58 $sku->getPropertyCollection()->setValues($fields['PROPERTIES']);
59 }
60
61 if (isset($fields['PRICES']))
62 {
63 $sku->getPriceCollection()->setValues($fields['PRICES']);
64 }
65
66 if (isset($fields['MEASURE']))
67 {
68 $sku->getMeasureRatioCollection()->setDefault($fields['MEASURE']);
69 }
70
71 if (isset($fields['BARCODES']))
72 {
76 $existBarcodes = [];
77
78 foreach ($sku->getBarcodeCollection() as $barcode)
79 {
80 $code = $barcode->getBarcode();
81 if ($code)
82 {
83 $existBarcodes[$code] = $barcode;
84 }
85 }
86
87 foreach ($fields['BARCODES'] as $code)
88 {
89 if (empty($code))
90 {
91 continue;
92 }
93
94 $existBarcode = $existBarcodes[$code] ?? null;
95 if ($existBarcode instanceof Barcode)
96 {
97 unset($existBarcodes[$code]);
98 }
99 else
100 {
101 $sku->getBarcodeCollection()->create()->setBarcode($code);
102 }
103 }
104
105 if ($existBarcodes)
106 {
107 $sku->getBarcodeCollection()->remove(...$existBarcodes);
108 }
109 }
110 }
111 }
112}