Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
skuprops.php
1<?
3
5
12{
19 protected static function filterByProps(array $skuProps, array $offersIds = array())
20 {
21 if(empty($offersIds))
22 return array();
23
24 if(!Loader::includeModule('iblock'))
25 return array();
26
27 $result = array();
28 $filter = array('ID' => $offersIds);
29
30 foreach($skuProps as $id => $value)
31 {
32 $propField = 'PROPERTY_'.$id;
33
34 if($value == '-')
35 $filter[$propField] = false;
36 else
37 $filter[$propField] = $value;
38 }
39
40 $res = \CIBlockElement::GetList(array("SORT"=>"ASC"), $filter, false, false, array('ID'));
41
42 while($el = $res->Fetch())
43 $result[] = $el['ID'];
44
45 return $result;
46 }
47
54 protected static function extractRequiredProps(array $skuProps, array $skuOrder, $changedSkuId)
55 {
56 $result = array();
57
58 foreach($skuOrder as $skuId)
59 {
60 if(!empty($skuProps[$skuId]))
61 $result[$skuId] = $skuProps[$skuId];
62
63 if($skuId == $changedSkuId)
64 break;
65 }
66
67 return $result;
68 }
69
76 protected static function extractOptionalProps(array $skuProps, array $skuOrder, $changedSkuId)
77 {
78 $result = array();
79 $changedAchieved = false;
80
81 foreach($skuOrder as $skuId)
82 {
83 if($skuId == $changedSkuId)
84 {
85 $changedAchieved = true;
86 continue;
87 }
88
89 if($skuId != $changedSkuId && !$changedAchieved)
90 continue;
91
92 if(!empty($skuProps[$skuId]))
93 $result[$skuId] = $skuProps[$skuId];
94 }
95
96 return $result;
97 }
98
107 public static function getProductId(array $skuProps, $productId, array $skuOrder, $changedSkuId)
108 {
109 if(
110 empty($skuProps)
111 || intval($productId) <= 0
112 || empty($skuOrder)
113 || intval($changedSkuId) <= 0
114 )
115 {
116 return 0;
117 }
118
119 $offersIds = self::getOffersIds($productId);
120 $requiredProps = self::extractRequiredProps($skuProps, $skuOrder, $changedSkuId);
121 $offersIds = self::filterByProps($requiredProps, $offersIds[$productId]);
122
123 if(count($offersIds) == 0)
124 return 0;
125
126 if(count($offersIds) == 1)
127 return current($offersIds);
128
129 $optionalProps = self::extractOptionalProps($skuProps, $skuOrder, $changedSkuId);
130
131 if(empty($optionalProps))
132 return current($offersIds);
133
134 foreach($optionalProps as $id => $val)
135 {
136 $prevProducts = $offersIds;
137 $offersIds = self::filterByProps(array($id => $val), $offersIds);
138
139 if(count($offersIds) == 0)
140 return current($prevProducts);
141
142 if(count($offersIds) == 1)
143 return current($offersIds);
144 }
145
146 return current($offersIds);
147 }
148
154 protected static function getOffersIds($productIds)
155 {
156 if(!Loader::includeModule('catalog'))
157 return array();
158
159 $result = array();
160 $offers = \CCatalogSKU::getOffersList($productIds, 0, array(), array('ID'));
161
162 if(is_array($offers))
163 {
164 foreach($offers as $productId => $items)
165 {
166 $result[$productId] ??= [];
167
168 if(!is_array($result[$productId]))
169 {
170 $result[$productId] = [];
171 }
172
173 foreach($items as $item)
174 {
175 $result[$productId][] = $item['ID'];
176 }
177 }
178 }
179
180 return $result;
181 }
182
190 protected static function getSkuPropValues($propId, $currentValue, array &$offersIds)
191 {
192 if(!Loader::includeModule('iblock'))
193 return array();
194
195 $result = array();
196 $foundElements = array();
197
198 $res = \CIBlockElement::GetList(
199 array("SORT"=>"ASC"),
200 array(
201 'ID' => $offersIds,
202 '!=PROPERTY_'.$propId => false
203 ),
204 false,
205 false,
206 array('ID', 'PROPERTY_'.$propId)
207 );
208
209 while($el = $res->Fetch())
210 {
211 if(isset($el['PROPERTY_'.$propId.'_ENUM_ID']))
212 $value = $el['PROPERTY_'.$propId.'_ENUM_ID'];
213 else
214 $value = $el['PROPERTY_'.$propId.'_VALUE'];
215
216 if(!in_array($value, $result))
217 $result[] = $value;
218
219 $foundElements[] = $el['ID'];
220
221 if($value != $currentValue)
222 {
223 $key = array_search($el['ID'], $offersIds);
224
225 if($key !== false)
226 unset($offersIds[$key]);
227 }
228 }
229
230 $foundElements = array_unique($foundElements, SORT_NUMERIC);
231 $valueLess = array_diff($offersIds, $foundElements);
232
233 if(!empty($valueLess))
234 {
235 if(!empty($result))
236 $result[] = '-';
237
238 if($currentValue != '-')
239 $offersIds = array_diff($offersIds, $valueLess);
240 }
241
242 return $result;
243 }
244
250 public static function getPossibleSkuPropsValues(array $params)
251 {
252 $result = array();
253 $productIds = array();
254
255 foreach($params as $param)
256 $productIds[] = $param['PRODUCT_ID'];
257
258 $productOffersIds = self::getOffersIds($productIds);
259
260 foreach($params as $param)
261 {
262 if(intval($param['PRODUCT_ID']) <= 0)
263 continue;
264
265 if(empty($productOffersIds[$param['PRODUCT_ID']]))
266 continue;
267
268 $offerId = intval($param['OFFER_ID']);
269
270 $result[$offerId] ??= [];
271 if (!is_array($result[$offerId]))
272 {
273 $result[$offerId] = [];
274 }
275
276 if(empty($param['SKU_PROPS']) || empty($param['SKU_ORDER']))
277 continue;
278
279 $offersIds = $productOffersIds[$param['PRODUCT_ID']];
280
281 foreach($param['SKU_ORDER'] as $propId)
282 {
283 if(empty($param['SKU_PROPS'][$propId]))
284 continue;
285
286 if(count($offersIds) > 0)
287 $res = self::getSkuPropValues($propId, $param['SKU_PROPS'][$propId], $offersIds);
288
289 if(count($offersIds) > 0)
290 {
291 if(!empty($res))
292 $result[$offerId][$propId] = $res;
293 }
294 else
295 {
296 $result[$offerId][$propId] = $param['SKU_PROPS'][$propId];
297 }
298 }
299 }
300
301 return $result;
302 }
303}
static getOffersIds($productIds)
Definition skuprops.php:154
static getSkuPropValues($propId, $currentValue, array &$offersIds)
Definition skuprops.php:190
static filterByProps(array $skuProps, array $offersIds=array())
Definition skuprops.php:19
static getProductId(array $skuProps, $productId, array $skuOrder, $changedSkuId)
Definition skuprops.php:107
static extractOptionalProps(array $skuProps, array $skuOrder, $changedSkuId)
Definition skuprops.php:76
static getPossibleSkuPropsValues(array $params)
Definition skuprops.php:250
static extractRequiredProps(array $skuProps, array $skuOrder, $changedSkuId)
Definition skuprops.php:54