Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productcategoryrestriction.php
1<?php
2
4
7use \Bitrix\Main\Localization\Loc;
9
10Loc::getMessage(__FILE__);
11
13{
19 abstract protected static function getJsHandler() : string;
20
26 abstract protected static function getBasketItems(Entity $entity) : array;
27
33 public static function extractParams(Entity $entity) : array
34 {
35 if (!\Bitrix\Main\Loader::includeModule('catalog'))
36 {
37 return [];
38 }
39
40 $basketItems = static::getBasketItems($entity);
41
42 $productIds = [];
43
45 foreach ($basketItems as $basketItem)
46 {
47 if ($basketItem->getField('MODULE') != 'catalog')
48 {
49 continue;
50 }
51
52 $productId = (int)$basketItem->getField('PRODUCT_ID');
53 $productInfo = \CCatalogSKU::getProductInfo($productId);
54
55 $candidate = $productInfo['ID'] ?? $productId;
56
57 if (!in_array($candidate, $productIds))
58 {
59 $productIds[] = $candidate;
60 }
61 }
62
63 return self::getCategoriesItems($productIds);
64 }
65
70 public static function getClassTitle() : string
71 {
72 return Loc::getMessage('SALE_BASE_RESTRICTION_BY_CATEGORY');
73 }
74
75 public static function getOnApplyErrorMessage(): string
76 {
77 return Loc::getMessage('SALE_BASE_RESTRICTION_BY_CATEGORY_ON_APPLY_ERROR_MSG');
78 }
79
88 public static function check($categoriesList, array $restrictionParams, $deliveryId = 0) : bool
89 {
90 if (
91 empty($categoriesList)
92 || !is_array($categoriesList)
93 || empty($restrictionParams["CATEGORIES"])
94 || !is_array($restrictionParams["CATEGORIES"])
95 )
96 {
97 return true;
98 }
99
100 foreach ($categoriesList as $productId => $productCategories)
101 {
102 if (!is_array($productCategories) || empty($productCategories))
103 {
104 continue;
105 }
106
107 $isProductFromCategory = false;
108
109 foreach ($productCategories as $categoryId)
110 {
111 $categoryPath = self::getCategoriesPath($categoryId);
112
113 if (array_intersect($categoryPath, $restrictionParams["CATEGORIES"]))
114 {
115 $isProductFromCategory = true;
116 break;
117 }
118 }
119
120 if (!$isProductFromCategory)
121 {
122 return false;
123 }
124 }
125
126 return true;
127 }
128
134 protected static function getCategoriesItems(array $productIds) : array
135 {
136 if (!\Bitrix\Main\Loader::includeModule('iblock'))
137 {
138 return [];
139 }
140
141 $groupsIds = [];
142
143 $res = \CIBlockElement::GetElementGroups($productIds, true, ['ID', 'IBLOCK_ELEMENT_ID']);
144
145 while ($group = $res->Fetch())
146 {
147 if (!is_array($groupsIds[$group['IBLOCK_ELEMENT_ID']]))
148 {
149 $groupsIds[$group['IBLOCK_ELEMENT_ID']] = [];
150 }
151
152 if (!in_array($group['ID'], $groupsIds[$group['IBLOCK_ELEMENT_ID']]))
153 {
154 $groupsIds[$group['IBLOCK_ELEMENT_ID']][] = $group['ID'];
155 }
156 }
157
158 return $groupsIds;
159 }
160
166 protected static function getCategoriesPath($categoryId) : array
167 {
168 if (!\Bitrix\Main\Loader::includeModule('iblock'))
169 {
170 return [];
171 }
172
173 $result = [];
174
175 $nav = \CIBlockSection::GetNavChain(false, $categoryId, ['ID'], true);
176 foreach ($nav as $arSectionPath)
177 {
178 $result[] = $arSectionPath['ID'];
179 }
180
181 return $result;
182 }
183
189 public static function getParamsStructure($entityId = 0) : array
190 {
191 return [
192 "CATEGORIES" => [
193 "TYPE" => "PRODUCT_CATEGORIES",
194 "ID" => 'sale-admin-category-restriction',
195 "JS_HANDLER" => static::getJsHandler(),
196 "LABEL" => Loc::getMessage('SALE_BASE_RESTRICTION_BY_CATEGORY_LST_LABEL'),
197 ],
198 ];
199 }
200
201
202}
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static check($categoriesList, array $restrictionParams, $deliveryId=0)
static extractParams(Entity $entity)