1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
SkuRepository.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\v2\Sku;
4
5use Bitrix\Catalog\ProductTable;
6use Bitrix\Catalog\v2\BaseIblockElementEntity;
7use Bitrix\Catalog\v2\BaseIblockElementRepository;
8use Bitrix\Catalog\v2\Iblock\IblockInfo;
9use Bitrix\Catalog\v2\Product\BaseProduct;
10use Bitrix\Catalog\v2\Product\ProductRepositoryContract;
11use Bitrix\Catalog\v2\Property\Property;
12use Bitrix\Catalog\v2\Property\PropertyCollection;
13use Bitrix\Catalog\v2\Property\PropertyRepositoryContract;
14
24{
26 protected $factory;
31
40 public function __construct(
45 )
46 {
47 parent::__construct($factory, $iblockInfo);
48 $this->productRepository = $productRepository;
49 $this->propertyRepository = $propertyRepository;
50 }
51
57 {
58 $callback = function (array $params) use ($product) {
59 yield from $this->getSkuIteratorForProduct($product, $params);
60 };
61
62 return $this->factory
63 ->createCollection()
64 ->setIteratorCallback($callback)
65 ;
66 }
67
76 public function getEntitiesByProduct(BaseProduct $product, array $params): \Generator
77 {
78 return $this->getSkuIteratorForProduct($product, $params);
79 }
80
86 {
87 $callback = function (array $params) use ($product) {
88 yield from $this->getSkuIteratorEagerLoading($product, $params);
89 };
90
91 return $this->factory
92 ->createCollection()
93 ->setIteratorCallback($callback)
94 ;
95 }
96
97 public function getEntitiesBy($params): array
98 {
99 $sku = parent::getEntitiesBy($params);
100
101 if (!empty($sku))
102 {
103 $this->loadParentProducts(...$sku);
104 }
105
106 return $sku;
107 }
108
109 protected function getAdditionalFilter(): array
110 {
111 $filter = parent::getAdditionalFilter();
112 $filter['IBLOCK_ID'] = $this->iblockInfo->getSkuIblockId();
113
114 return $filter;
115 }
116
117 protected function getAdditionalProductFilter(): array
118 {
119 $filter = parent::getAdditionalProductFilter();
120
121 $filter['=TYPE'] = [
125 ];
126
127 return $filter;
128 }
129
131 {
132 $type = (int)($fields['TYPE'] ?? 0);
133
135 {
136 $entityClass = $this->factory::SKU;
137 }
138 else
139 {
140 $entityClass = $this->factory::SIMPLE_SKU;
141 }
142
143 return $this->factory->createEntity($entityClass);
144 }
145
146 private function loadParentProducts(BaseSku ...$skuItems): void
147 {
148 $skuByProductMap = $this->getSkuByProductMap($skuItems);
149
150 if (!empty($skuByProductMap))
151 {
152 $products = $this->productRepository->getEntitiesBy([
153 'filter' => [
154 '=ID' => array_keys($skuByProductMap),
155 ],
156 ]);
157
159 foreach ($products as $product)
160 {
161 $productSkuItems = $skuByProductMap[$product->getId()] ?? [];
162 $skuCollection = $this->getCollectionByProduct($product)
163 ->setParent($product)
164 ->add(...$productSkuItems)
165 ;
166 $product->setSkuCollection($skuCollection);
167 }
168 }
169 }
170
171 private function getSkuByProductMap(array $skuItems): array
172 {
173 $skuByProductMap = [];
174
175 $skuMap = [];
177 foreach ($skuItems as $sku)
178 {
179 if ($sku->getParent() === null)
180 {
181 $skuMap[$sku->getId()] = $sku;
182 }
183 }
184
185 if (!empty($skuMap))
186 {
187 $skuPropertyId = $this->iblockInfo->getSkuPropertyId();
188 $propertyValuesIterator = \CIBlockElement::GetPropertyValues(
189 $this->iblockInfo->getSkuIblockId(),
190 ['ID' => array_keys($skuMap)],
191 false,
192 ['ID' => $skuPropertyId]
193 );
194
195 while ($propertyValues = $propertyValuesIterator->fetch())
196 {
197 $productId = $propertyValues[$skuPropertyId];
198 $sku = $skuMap[$propertyValues['IBLOCK_ELEMENT_ID']];
199
200 $skuByProductMap[$productId][] = $sku;
201 }
202 }
203
204 return $skuByProductMap;
205 }
206
207 private function getSkuIteratorForProduct(BaseProduct $product, array $params = []): \Generator
208 {
209 if ($product->isSimple())
210 {
211 if ($product->getSkuCollection()->isEmpty())
212 {
213 yield $this->createEntity();
214 }
215 }
216 elseif (!$product->isNew())
217 {
218 $params['filter']['PROPERTY_' . $this->iblockInfo->getSkuPropertyId()] = $product->getId();
219 $params['order']['ID'] ??= 'DESC';
220
221 foreach ($this->getList($params) as $item)
222 {
223 yield $this->createEntity($item);
224 }
225 }
226 }
227
228 private function getSkuIteratorEagerLoading(BaseProduct $product, array $params = []): \Generator
229 {
230 if ($product->isSimple())
231 {
232 if ($product->getSkuCollection()->isEmpty())
233 {
234 yield $this->createEntity();
235 }
236 }
237 elseif (!$product->isNew())
238 {
239 $params['filter']['PROPERTY_' . $this->iblockInfo->getSkuPropertyId()] = $product->getId();
240 $params['order']['ID'] = 'DESC';
241
242 $items = [];
243 foreach ($this->getList($params) as $item)
244 {
245 $items[$item['ID']] = $item;
246 }
247
248 $skuIds = array_keys($items);
249
250 $propertySettings = $this->propertyRepository->getPropertiesSettingsByFilter([
251 '=IBLOCK_ID' => $this->iblockInfo->getSkuIblockId(),
252 ]);
253
254 $propertyElementMap = $this->getPropertyMapBySkuIds($skuIds, $propertySettings);
255
256 foreach ($items as $skuId => $item)
257 {
258 $propertyCollection = $this->propertyRepository->createCollection();
259
260 foreach ($propertySettings as $setting)
261 {
262 $propertyItem =
263 $propertyElementMap[$skuId][$setting['ID']]
264 ?? $this->propertyRepository->createEntity([], $setting)
265 ;
266
267 if ($propertyItem)
268 {
269 $propertyCollection->add($propertyItem);
270 }
271 }
272
273 yield $this->createEntity($item, $propertyCollection);
274 }
275 }
276 }
277
283 private function getPropertyMapBySkuIds(array $skuIds, array $propertySettings): array
284 {
285 $skuPropertyFilter = [
286 'filter' => [
287 'IBLOCK_ID' => $this->iblockInfo->getSkuIblockId(),
288 'ID' => $skuIds,
289 ],
290 ];
291
292 $properties = $this->propertyRepository->getEntitiesBy($skuPropertyFilter, $propertySettings);
293 $propertyElementMap = [];
294
296 foreach ($properties as $property)
297 {
298 $elementId = $property->getSetting('IBLOCK_ELEMENT_ID');
299
300 if ($elementId > 0)
301 {
302 $propertyElementMap[$elementId] = $propertyElementMap[$elementId] ?? [];
303 $propertyElementMap[$elementId][$property->getSetting('ID')] = $property;
304 }
305 }
306
307 return $propertyElementMap;
308 }
309
310 protected function createEntity(array $fields = [], PropertyCollection $propertyCollection = null): BaseIblockElementEntity
311 {
312 $entity = parent::createEntity($fields);
313
314 if ($propertyCollection)
315 {
316 $entity->setPropertyCollection($propertyCollection);
317 }
318
319 return $entity;
320 }
321
323 {
324 if (isset($this->productRepository))
325 {
326 if ($this->productRepository->getDetailUrlTemplate() === null)
327 {
328 $this->productRepository->setDetailUrlTemplate($template);
329 $this->productRepository->setAutoloadDetailUrl($template !== null);
330 }
331 }
332
333 return parent::setDetailUrlTemplate($template);
334 }
335
337 {
338 if (isset($this->productRepository))
339 {
340 $this->productRepository->setAutoloadDetailUrl($state);
341 }
342
343 return parent::setAutoloadDetailUrl($state);
344 }
345
346 public function getCountByProductId(int $productId): int
347 {
348 $filter = [
349 'PROPERTY_' . $this->iblockInfo->getSkuPropertyId() => $productId,
350 ];
351
352 return \CIBlockElement::GetList(
353 [],
354 array_merge(
355 [
356 // 'ACTIVE' => 'Y',
357 // 'ACTIVE_DATE' => 'Y',
358 ],
359 $filter,
360 $this->getAdditionalFilter()
361 ),
362 []
363 );
364 }
365}
$type
Определения options.php:106
const TYPE_FREE_OFFER
Определения product.php:74
const TYPE_OFFER
Определения product.php:73
const TYPE_PRODUCT
Определения product.php:70
setIteratorCallback(\Closure $iteratorCallback)
Определения BaseCollection.php:301
__construct(SkuFactory $factory, IblockInfo $iblockInfo, ProductRepositoryContract $productRepository, PropertyRepositoryContract $propertyRepository)
Определения SkuRepository.php:40
loadEagerCollectionByProduct(BaseProduct $product)
Определения SkuRepository.php:85
getCountByProductId(int $productId)
Определения SkuRepository.php:346
getEntitiesByProduct(BaseProduct $product, array $params)
Определения SkuRepository.php:76
createEntity(array $fields=[], PropertyCollection $propertyCollection=null)
Определения SkuRepository.php:310
setDetailUrlTemplate(?string $template)
Определения SkuRepository.php:322
getCollectionByProduct(BaseProduct $product)
Определения SkuRepository.php:56
makeEntity(array $fields=[])
Определения SkuRepository.php:130
setAutoloadDetailUrl(bool $state)
Определения SkuRepository.php:336
$template
Определения file_edit.php:49
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$entity
$filter
Определения iblock_catalog_list.php:54
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if($inWords) echo htmlspecialcharsbx(Number2Word_Rus(roundEx($totalVatSum $params['CURRENCY']
Определения template.php:799
$items
Определения template.php:224
$fields
Определения yandex_run.php:501