Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productlist.php
1<?php
2
4
8
9abstract class ProductList extends \CBitrixComponent
10{
11 protected $measures = [];
12
13 protected function loadCatalog(array $skuIds): array
14 {
15 Main\Type\Collection::normalizeArrayValuesByInt($skuIds, true);
16
17 $repositoryFacade = ServiceContainer::getRepositoryFacade();
18 if (!$repositoryFacade)
19 {
20 return [];
21 }
22
23 $productInfo = [];
24 $productSkuIblockMap = [];
25 foreach ($skuIds as $skuId)
26 {
27 $sku = $repositoryFacade->loadVariation($skuId);
28 if (!$sku)
29 {
30 continue;
31 }
32
34 $product = $sku->getParent();
35
36 $fields = $sku->getFields();
37 $fields['PRODUCT_ID'] = $product->getId();
38 $fields['SKU_ID'] = $skuId;
39 $fields['OFFERS_IBLOCK_ID'] = 0;
40 $fields['SKU_TREE'] = [];
41 $fields['DETAIL_URL'] = $this->getElementDetailUrl($product->getIblockId(), $product->getId());
42 $fields['TYPE'] = (int)$fields['TYPE'];
43
44 $measure = $this->measures[$sku->getField('MEASURE')] ?? null;
45 if (!$measure)
46 {
47 $measure = $this->getDefaultMeasure();
48 }
49
50 $fields['MEASURE_CODE'] = $measure['CODE'];
51 $fields['MEASURE_NAME'] = $measure['SYMBOL'];
52
53 if (!$product->isSimple())
54 {
55 $fields['OFFERS_IBLOCK_ID'] = $fields['IBLOCK_ID'];
56 $fields['IBLOCK_ID'] = $product->getIblockId();
57 $productSkuIblockMap[$product->getIblockId()] = $productSkuIblockMap[$product->getIblockId()] ?? [];
58 $productSkuIblockMap[$product->getIblockId()][$product->getId()][] = $sku->getId();
59 }
60
61 $productInfo[$skuId] = [
62 'SKU' => $sku,
63 'FIELDS' => $fields,
64 ];
65 }
66
67 if ($productSkuIblockMap)
68 {
69 foreach ($productSkuIblockMap as $iblockId => $productMap)
70 {
71 $skuTree = ServiceContainer::make('sku.tree', ['iblockId' => $iblockId]);
72 if ($skuTree)
73 {
74 $skuTreeItems = $skuTree->loadJsonOffers($productMap);
75 foreach ($skuTreeItems as $offers)
76 {
77 foreach ($offers as $skuId => $skuTreeItem)
78 {
79 if (isset($productInfo[$skuId]['FIELDS']))
80 {
81 $productInfo[$skuId]['FIELDS']['SKU_TREE'] = $skuTreeItem;
82 }
83 }
84 }
85 }
86 }
87 }
88
89 return $productInfo;
90 }
91
92 protected function getElementDetailUrl(int $iblockId, int $skuId = 0): string
93 {
94 $urlBuilder = BuilderManager::getInstance()->getBuilder($this->arParams['BUILDER_CONTEXT']);
95 if (!$urlBuilder)
96 {
97 return '';
98 }
99
100 $urlBuilder->setIblockId($iblockId);
101 return $urlBuilder->getElementDetailUrl($skuId);
102 }
103
104 protected function loadMeasures(): void
105 {
106 $measureResult = \CCatalogMeasure::getList(
107 ['CODE' => 'ASC'],
108 [],
109 false,
110 [],
111 ['CODE', 'SYMBOL_RUS', 'SYMBOL_INTL', 'IS_DEFAULT', 'ID']
112 );
113
114 $this->measures = [];
115 while ($measureFields = $measureResult->Fetch())
116 {
117 $measureItem = [
118 'ID' => $measureFields['ID'],
119 'CODE' => $measureFields['CODE'],
120 'IS_DEFAULT' => $measureFields['IS_DEFAULT'],
121 'SYMBOL' => $measureFields['SYMBOL_RUS'] ?? $measureFields['SYMBOL_INTL'],
122 ];
123
124 $this->measures[$measureFields['ID']] = $measureItem;
125 }
126 }
127
131 protected function getDefaultMeasure(): array
132 {
133 return \CCatalogMeasure::getDefaultMeasure(true);
134 }
135}
getElementDetailUrl(int $iblockId, int $skuId=0)