Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PropertyValue.php
1<?php
2
4
5use Bitrix\Catalog\Product\PropertyCatalogFeature;
8
17final class PropertyValue
18{
23 public static function getSkuPropertyDisplayValues(BaseSku $sku): string
24 {
25 if ($sku->isSimple())
26 {
27 return '';
28 }
29
30 $propertyValues = self::getPropertyValues(
31 $sku->getIblockId(),
32 [$sku->getId()]
33 );
34
35 $skuPropertyValues = $propertyValues[$sku->getId()] ?? [];
36
37 return self::getPropertyDisplayValues($skuPropertyValues);
38 }
39
44 public static function getSkuPropertyDisplayValuesMap(BaseSku $sku): array
45 {
46 if ($sku->isSimple())
47 {
48 return [];
49 }
50
51 $propertyValues = self::getPropertyValues(
52 $sku->getIblockId(),
53 [$sku->getId()]
54 );
55
56 $skuPropertyValues = $propertyValues[$sku->getId()] ?? [];
57
58 $result = [];
59
60 foreach ($skuPropertyValues as $property)
61 {
62 $displayValue = self::getPropertyDisplayValue($property);
63 if (!$displayValue)
64 {
65 continue;
66 }
67
68 $property['DISPLAY_VALUE'] = $displayValue;
69
70 $result[$property['CODE']] = $property;
71 }
72
73 return $result;
74 }
75
81 private static function getPropertyValues($skuIblockId, array $skuIds): array
82 {
83 $propertyValues = array_fill_keys($skuIds, []);
84
85 \CIBlockElement::GetPropertyValuesArray(
86 $propertyValues,
87 $skuIblockId,
88 ['ID' => $skuIds],
89 ['ID' => PropertyCatalogFeature::getOfferTreePropertyCodes($skuIblockId)]
90 );
91
92 return $propertyValues;
93 }
94
99 public static function getPropertyValuesBySku(BaseSku $sku): array
100 {
101 return self::getPropertyValues($sku->getIblockId(), [$sku->getId()])[$sku->getId()];
102 }
103
108 private static function getPropertyDisplayValues(array $properties): string
109 {
110 $result = [];
111
112 foreach ($properties as $property)
113 {
114 $displayValue = self::getPropertyDisplayValue($property);
115 if (!$displayValue)
116 {
117 continue;
118 }
119
120 $result[] = $displayValue;
121 }
122
123 return implode(', ', $result);
124 }
125
130 public static function getPropertyDisplayValue(array $propertyValue): string
131 {
132 if (!empty($propertyValue['USER_TYPE']))
133 {
134 $userType = \CIBlockProperty::GetUserType($propertyValue['USER_TYPE']);
135 $searchMethod = $userType['GetSearchContent'] ?? null;
136
137 if ($searchMethod && is_callable($searchMethod))
138 {
139 $value = $searchMethod($propertyValue, ['VALUE' => $propertyValue['~VALUE']], []);
140 }
141 else
142 {
143 $value = '';
144 }
145 }
146 else
147 {
148 $value = $propertyValue['~VALUE'] ?? '';
149 }
150
151 if (is_array($value))
152 {
153 $value = implode(', ', $value);
154 }
155
156 $value = trim((string)$value);
157
158 return $value;
159 }
160}
static getPropertyDisplayValue(array $propertyValue)
static getSkuPropertyDisplayValues(BaseSku $sku)
static getSkuPropertyDisplayValuesMap(BaseSku $sku)