Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalogjsproductform.php
1<?php
2
4
9
11{
12 public static function convertToBuilderFormat(array $params) : array
13 {
14 $result = [];
15
16 foreach ($params as $item)
17 {
18 if (empty($item['code']))
19 {
20 $item['code'] = 'n'.(count($result) + 1);
21 }
22
23 $product = self::obtainProductFields($item);
24
25 $result[$product['BASKET_CODE']] = $product;
26 }
27
28 return $result;
29 }
30
41 private static function consistentFields(array $fields): array
42 {
43 // prices
44 if (!empty($fields['discount']))
45 {
46 $price = (float)($fields['priceExclusive'] ?? $fields['price']);
47 $basePrice = (float)$fields['basePrice'];
48 $discountPrice = (float)$fields['discount'];
49
50 $realDiscountPrice = $basePrice - $price;
51 if ($discountPrice !== $realDiscountPrice)
52 {
53 $fields['discount'] = $realDiscountPrice;
54
55 if (isset($fields['discountRate']))
56 {
57 $fields['discountRate'] =
58 $basePrice > 0
59 ? $realDiscountPrice / $basePrice * 100
60 : 0
61 ;
62 }
63 }
64 }
65
66 return $fields;
67 }
68
74 protected static function obtainProductFields($fields) : array
75 {
76 $fields = self::consistentFields($fields);
77
78 $priceExclusive = $fields['priceExclusive'] ?? $fields['price'] ?? 0;
79 $basePrice = $fields['basePrice'] ?? 0;
80
81 $item = [
82 'QUANTITY' => (float)$fields['quantity'] > 0 ? (float)$fields['quantity'] : 1,
83 'PRODUCT_PROVIDER_CLASS' => '',
84 'BASKET_CODE' => $fields['code'] ?? '',
85 'PRODUCT_ID' => $fields['skuId'] ?? $fields['productId'] ?? 0,
86 'BASE_PRICE' => $basePrice,
87 'PRICE' => $priceExclusive,
88 'DISCOUNT_PRICE' => 0,
89 'ORIGIN_BASKET_ID' => (int)($fields['additionalFields']['originBasketId'] ?? 0),
90 'ORIGIN_PRODUCT_ID' => (int)($fields['additionalFields']['originProductId'] ?? 0),
91 'MANUALLY_EDITED' => 'Y',
92 'XML_ID' => $fields['innerId'],
93 'TYPE' => null,
94 ];
95
96 if (isset($fields['type']))
97 {
98 $type = (int)$fields['type'];
99
100 $item['TYPE'] = Internals\Catalog\ProductTypeMapper::getType($type);
101 }
102
103 if (isset($fields['name']))
104 {
105 $item['NAME'] = $fields['name'];
106 }
107
108 if (isset($fields['sort']))
109 {
110 $item['SORT'] = (int)$fields['sort'];
111 }
112
113 if (isset($fields['isCustomPrice']))
114 {
115 $item['CUSTOM_PRICE'] = $fields['isCustomPrice'] === 'Y' ? 'Y' : 'N';
116 }
117
118 if (isset($fields['measureName']))
119 {
120 $item['MEASURE_NAME'] = $fields['measureName'];
121 }
122
123 if (isset($fields['measureCode']))
124 {
125 $item['MEASURE_CODE'] = (int)$fields['measureCode'];
126 }
127
128 if (isset($fields['weight']))
129 {
130 $item['WEIGHT'] = $fields['weight'];
131 }
132
133 if (isset($fields['dimensions']))
134 {
135 $item['DIMENSIONS'] = $fields['dimensions'];
136 }
137
138 if (
139 isset($fields['taxIncluded'], $fields['taxId'])
140 && Main\Loader::includeModule('catalog')
141 )
142 {
143 $rateRow = VatTable::getRowById((int)$fields['taxId']);
144 if ($rateRow)
145 {
146 $item['VAT_RATE'] =
147 isset($rateRow['RATE'])
148 ? (float)$rateRow['RATE'] / 100
149 : null
150 ;
151 }
152
153 $item['VAT_INCLUDED'] = $fields['taxIncluded'] === 'N' ? 'N' : 'Y';
154 }
155
156 if (isset($fields['skuId']) && $fields['skuId'])
157 {
158 $item['PRODUCT_ID'] = $fields['skuId'];
159 }
160
161 if (
162 isset($fields['module'])
163 && $fields['module'] === 'catalog'
164 && Main\Loader::includeModule('catalog')
165 )
166 {
167 $item['MODULE'] = 'catalog';
168 $item['PRODUCT_PROVIDER_CLASS'] = Product\Basket::getDefaultProviderName();
169 }
170
171 if (
172 (int)$fields['discount'] === 0
173 && abs($priceExclusive - $basePrice) > 1e-10
174 && (float)$basePrice > 0
175 )
176 {
177 $fields['discount'] = (int)(100 - ($priceExclusive / $basePrice) * 100);
178 }
179
180 if ($fields['discount'] > 0)
181 {
182 $item['DISCOUNT_PRICE'] = $fields['discount'];
183 $item['CUSTOM_PRICE'] = 'Y';
184 $item['PRICE'] = $item['BASE_PRICE'] - $item['DISCOUNT_PRICE'];
185 }
186
187 if (isset($fields['properties']))
188 {
189 $allowedProps = self::getAllowedBasketProperties((int)$item['PRODUCT_ID']);
190 $item['PROPS'] = [];
191 foreach ($fields['properties'] as $property)
192 {
193 if (!in_array($property['CODE'], $allowedProps))
194 {
195 continue;
196 }
197
198 $value = '';
199
200 if (
201 !empty($property['PROPERTY_VALUES'])
202 && is_array($property['PROPERTY_VALUES'])
203 )
204 {
205 $value = current($property['PROPERTY_VALUES']);
206 $value = $value['DISPLAY_VALUE'] ?? '';
207 }
208
209 $item['PROPS'][] = [
210 'NAME' => $property['NAME'],
211 'SORT' => $property['SORT'],
212 'CODE' => $property['CODE'],
213 'VALUE' => $value,
214 ];
215 }
216 }
217
218 $item['FIELDS_VALUES'] = Main\Web\Json::encode($item);
219
220 return $item;
221 }
222
227 public static function getAllowedBasketProperties(int $productId): array
228 {
229 if (!Main\Loader::includeModule('catalog') || !Main\Loader::includeModule('iblock'))
230 {
231 return [];
232 }
233
234 $product = \CIBlockElement::GetList([], ['=ID' => $productId], false, false, ['IBLOCK_ID'])->Fetch();
235 $iblockId = 0;
236 if (is_array($product) && isset($product['IBLOCK_ID']))
237 {
238 $iblockId = (int)$product['IBLOCK_ID'];
239 }
240
241 if ($iblockId <= 0)
242 {
243 return [];
244 }
245
246 static $allowedPropertyCodes = [];
247 if (!array_key_exists($iblockId, $allowedPropertyCodes))
248 {
249 $propertyCodes = Product\PropertyCatalogFeature::getBasketPropertyCodes($iblockId, ['CODE' => 'Y']);
250 $allowedPropertyCodes[$iblockId] = is_array($propertyCodes) ? $propertyCodes : [];
251 }
252
253 return $allowedPropertyCodes[$iblockId];
254 }
255}