1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
offer.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Controller\Product;
4
5use Bitrix\Catalog\Controller\ErrorCode;
6use Bitrix\Catalog\ProductTable;
7use Bitrix\Main\Error;
8use Bitrix\Main\Result;
9
10final class Offer extends Base
11{
12 protected const TYPE = ProductTable::TYPE_OFFER;
13
14 private static array $parentProductCache = [];
15
16 protected function getAllowedProductTypes(): array
17 {
18 return [
21 ];
22 }
23
24 protected function prepareFieldsForAdd(array $fields): ?array
25 {
26 $fields = parent::prepareFieldsForAdd($fields);
27 if ($fields === null)
28 {
29 return null;
30 }
31
32 $iblockId = (int)($fields['IBLOCK_ID'] ?? 0);
33 if ($iblockId <= 0)
34 {
35 return null;
36 }
37
39 $view = $this->getViewManager()->getView($this);
40
41 $result = $view->getCatalogDescription($iblockId);
42 if (!$result->isSuccess())
43 {
44 $this->addErrors($result->getErrors());
45
46 return null;
47 }
48
49 $catalog = $result->getData();
50 unset($result);
51
52 $skuPropertyId = 'PROPERTY_' . $catalog['SKU_PROPERTY_ID'];
53
54 $result = $this->prepareParentId($fields[$skuPropertyId] ?? null, $catalog['PRODUCT_IBLOCK_ID']);
55 if (!$result->isSuccess())
56 {
57 $this->addErrors($result->getErrors());
58
59 return null;
60 }
61
62 $parentId = ($result->getData()['PARENT_ID']) ?? null;
63 $fields[$skuPropertyId] = $parentId;
64 if ($fields[$skuPropertyId] === null)
65 {
66 unset($fields[$skuPropertyId]);
67 }
69
70 return $fields;
71 }
72
73 protected function getParentProduct(int $id): ?array
74 {
75 if (isset(self::$parentProductCache[$id]))
76 {
77 return self::$parentProductCache[$id] !== false ? self::$parentProductCache[$id] : null;
78 }
79
80 self::$parentProductCache[$id] = false;
81 $result = Producttable::getRow([
82 'select' => [
83 'ID',
84 'TYPE',
85 'IBLOCK_ID' => 'IBLOCK_ELEMENT.IBLOCK_ID',
86 ],
87 'filter' => [
88 '=ID' => $id,
89 ]
90 ]);
91 if ($result === null)
92 {
93 return null;
94 }
95
96 $result['ID'] = (int)$result['ID'];
97 $result['TYPE'] = (int)$result['TYPE'];
98 $result['IBLOCK_ID'] = (int)$result['IBLOCK_ID'];
99
100 self::$parentProductCache[$id] = $result;
101
102 return $result;
103 }
104
105 protected function prepareParentId(mixed $parentId, int $iblockId): Result
106 {
107 $emptyParent = ['PARENT_ID' => null];
108 $result = new Result();
109
110 if ($parentId === null)
111 {
112 $result->setData($emptyParent);
113
114 return $result;
115 }
116
117 $parentId =
118 is_array($parentId) && isset($parentId['VALUE'])
119 ? (int)$parentId['VALUE']
120 : (int)$parentId
121 ;
122 if ($parentId <= 0)
123 {
124 $result->setData($emptyParent);
125
126 return $result;
127 }
128
129 $row = $this->getParentProduct($parentId);
130 if ($row === null)
131 {
132 $result->addError(new Error(
133 'Parent product not found.',
135 ));
136
137 return $result;
138 }
139
140 if (
141 $row['TYPE'] !== ProductTable::TYPE_PRODUCT
142 && $row['TYPE'] !== ProductTable::TYPE_SKU
143 && $row['TYPE'] !== ProductTable::TYPE_EMPTY_SKU
144 )
145 {
146 $result->addError(new Error(
147 'Invalid parent product type.',
149 ));
150
151 return $result;
152 }
153
154 if ($row['IBLOCK_ID'] !== $iblockId)
155 {
156 $result->addError(new Error(
157 'Invalid information block of the parent product.',
159 ));
160
161 return $result;
162 }
163
164 $result->setData(['PARENT_ID' => $row['ID']]);
165
166 return $result;
167 }
168}
const PRODUCT_OFFER_BAD_PARENT_TYPE
Определения errorcode.php:16
const PRODUCT_OFFER_PARENT_NOT_FOUND
Определения errorcode.php:15
const PRODUCT_OFFER_BAD_PARENT_IBLOCK_ID
Определения errorcode.php:17
prepareParentId(mixed $parentId, int $iblockId)
Определения offer.php:105
getParentProduct(int $id)
Определения offer.php:73
prepareFieldsForAdd(array $fields)
Определения product.php:1069
const TYPE_EMPTY_SKU
Определения product.php:75
const TYPE_SKU
Определения product.php:72
const TYPE_FREE_OFFER
Определения product.php:74
const TYPE_OFFER
Определения product.php:73
const TYPE_PRODUCT
Определения product.php:70
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$iblockId
Определения iblock_catalog_edit.php:30
$catalog
Определения iblock_catalog_edit.php:135
$fields
Определения yandex_run.php:501