Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exportoffersku.php
1<?php
2namespace Bitrix\Catalog\Ebay;
3
5
7{
8 protected $arSKUExport = array();
10 protected $arProperties;
11 protected $arOfferIBlock = array();
12 protected $arOffers = array();
13 protected $intOfferIBlockID =0;
14
15 public function __construct($catalogType, $params)
16 {
17 parent::__construct($catalogType, $params);
18
19 $this->arSKUExport = $this->getSKUExport();
20 $this->arOffers = $this->getOffers();
21 }
22
23 protected function getOffers()
24 {
25 $arPropertyMap = array();
26 $arSelectedPropTypes = array('S','N','L','E','G');
27 $this->arSelectOfferProps = array();
28 $arOffers = \CCatalogSku::GetInfoByProductIBlock($this->iBlockId);
29
30 if (empty($arOffers['IBLOCK_ID']))
31 return array();
32
33 $this->intOfferIBlockID = $arOffers['IBLOCK_ID'];
34 $rsOfferIBlocks = \CIBlock::GetByID($this->intOfferIBlockID);
35
36 if (!$this->arOfferIBlock = $rsOfferIBlocks->Fetch())
37 throw new SystemException("Bad offers iBlock ID (".__CLASS__."::".__METHOD__.")");
38
39 $rsProps = \CIBlockProperty::GetList(
40 array('SORT' => 'ASC', 'NAME' => 'ASC'),
41 array('IBLOCK_ID' => $this->intOfferIBlockID, 'ACTIVE' => 'Y', 'CHECK_PERMISSIONS' => 'N')
42 );
43
44 while ($arProp = $rsProps->Fetch())
45 {
46 $arProp['ID'] = (int)$arProp['ID'];
47
48 if ($arOffers['SKU_PROPERTY_ID'] != $arProp['ID'])
49 {
50 $arProp['USER_TYPE'] = (string)$arProp['USER_TYPE'];
51 $arProp['CODE'] = (string)$arProp['CODE'];
52 $this->arIblock['OFFERS_PROPERTY'][$arProp['ID']] = $arProp;
53 $this->arProperties[$arProp['ID']] = $arProp;
54
55 if (in_array($arProp['PROPERTY_TYPE'], $arSelectedPropTypes))
56 $this->arSelectOfferProps[] = $arProp['ID'];
57
58 if ($arProp['CODE'] !== '')
59 {
60 foreach ($this->arIblock['PROPERTY'] as &$arMainProp)
61 {
62 if ($arMainProp['CODE'] == $arProp['CODE'])
63 {
64 $arPropertyMap[$arProp['ID']] = $arMainProp['CODE'];
65 break;
66 }
67 }
68
69 if (isset($arMainProp))
70 unset($arMainProp);
71 }
72 }
73 }
74
75 $this->arOfferIBlock['LID'] = $this->arIblock['LID'];
76
77 $this->arOfferIBlock['PROPERTY'] = array();
78
79 $rsProps = \CIBlockProperty::GetList(
80 array('SORT' => 'ASC', 'NAME' => 'ASC'),
81 array('IBLOCK_ID' => $this->intOfferIBlockID, 'ACTIVE' => 'Y', 'CHECK_PERMISSIONS' => 'N')
82 );
83
84 while ($arProp = $rsProps->Fetch())
85 {
86
87 $arProp['ID'] = (int)$arProp['ID'];
88 $arProp['USER_TYPE'] = (string)$arProp['USER_TYPE'];
89 $arProp['CODE'] = (string)$arProp['CODE'];
90 $this->arOfferIBlock['PROPERTY'][$arProp['ID']] = $arProp;
91 }
92
93 return $arOffers;
94 }
95
96 protected function getSKUExport()
97 {
98 $arOffersSelectKeys = array(
99 YANDEX_SKU_EXPORT_ALL,
100 YANDEX_SKU_EXPORT_MIN_PRICE,
101 YANDEX_SKU_EXPORT_PROP,
102 );
103
104 $arCondSelectProp = array(
105 'ZERO',
106 'NONZERO',
107 'EQUAL',
108 'NONEQUAL',
109 );
110
111 $arSKUExport = array();
112
113 if (is_array($this->arOfferIBlock) && !empty($this->arOfferIBlock))
114 {
115 if (empty($this->xmlData['SKU_EXPORT']))
116 throw new SystemException("YANDEX_ERR_SKU_SETTINGS_ABSENT");
117
118 $arSKUExport = $this->xmlData['SKU_EXPORT'];
119
120 if (empty($arSKUExport['SKU_EXPORT_COND']) || !in_array($arSKUExport['SKU_EXPORT_COND'], $arOffersSelectKeys))
121 throw new SystemException("YANDEX_SKU_EXPORT_ERR_CONDITION_ABSENT");
122
123 if (YANDEX_SKU_EXPORT_PROP == $arSKUExport['SKU_EXPORT_COND'])
124 {
125 if (empty($arSKUExport['SKU_PROP_COND']) || !is_array($arSKUExport['SKU_PROP_COND']))
126 throw new SystemException("YANDEX_SKU_EXPORT_ERR_PROPERTY_ABSENT");
127
128 if (empty($arSKUExport['SKU_PROP_COND']['PROP_ID']) || !in_array($arSKUExport['SKU_PROP_COND']['PROP_ID'],$this->arSelectOfferProps))
129 throw new SystemException("YANDEX_SKU_EXPORT_ERR_PROPERTY_ABSENT");
130
131 if (empty($arSKUExport['SKU_PROP_COND']['COND']) || !in_array($arSKUExport['SKU_PROP_COND']['COND'],$arCondSelectProp))
132 throw new SystemException("YANDEX_SKU_EXPORT_ERR_PROPERTY_COND_ABSENT");
133
134 if ($arSKUExport['SKU_PROP_COND']['COND'] == 'EQUAL' || $arSKUExport['SKU_PROP_COND']['COND'] == 'NONEQUAL')
135 {
136 if (empty($arSKUExport['SKU_PROP_COND']['VALUES']))
137 throw new SystemException("YANDEX_SKU_EXPORT_ERR_PROPERTY_VALUES_ABSENT");
138 }
139 }
140 }
141
142 return $arSKUExport;
143 }
144
145 protected function getOfferTemplateUrl()
146 {
147 $strOfferTemplateURL = '';
148
149 if (!empty($this->arSKUExport['SKU_URL_TEMPLATE_TYPE']))
150 {
151 switch($this->arSKUExport['SKU_URL_TEMPLATE_TYPE'])
152 {
153 case YANDEX_SKU_TEMPLATE_PRODUCT:
154 $strOfferTemplateURL = '#PRODUCT_URL#';
155 break;
156 case YANDEX_SKU_TEMPLATE_CUSTOM:
157 if (!empty($this->arSKUExport['SKU_URL_TEMPLATE']))
158 $strOfferTemplateURL = $this->arSKUExport['SKU_URL_TEMPLATE'];
159 break;
160 case YANDEX_SKU_TEMPLATE_OFFERS:
161 default:
162 $strOfferTemplateURL = '';
163 break;
164 }
165 }
166
167 return $strOfferTemplateURL;
168 }
169
170 protected function getOffersItemsDb($itemId)
171 {
172 $arOfferSelect = array("ID", "LID", "IBLOCK_ID", "NAME", "PREVIEW_PICTURE", "PREVIEW_TEXT",
173 "PREVIEW_TEXT_TYPE", "DETAIL_PICTURE", "DETAIL_PAGE_URL", "DETAIL_TEXT");
174
175 $arOfferFilter = array('IBLOCK_ID' => $this->intOfferIBlockID, 'PROPERTY_'.$this->arOffers['SKU_PROPERTY_ID'] => 0,
176 "ACTIVE" => "Y", "ACTIVE_DATE" => "Y");
177
178 if($this->onlyAvailableElements)
179 $arOfferFilter["CATALOG_AVAILABLE"] = "Y";
180
181 if (YANDEX_SKU_EXPORT_PROP == $this->arSKUExport['SKU_EXPORT_COND'])
182 {
183 $strExportKey = '';
184 $mxValues = false;
185
186 if ($this->arSKUExport['SKU_PROP_COND']['COND'] == 'NONZERO' || $this->arSKUExport['SKU_PROP_COND']['COND'] == 'NONEQUAL')
187 $strExportKey = '!';
188
189 $strExportKey .= 'PROPERTY_'.$this->arSKUExport['SKU_PROP_COND']['PROP_ID'];
190
191 if ($this->arSKUExport['SKU_PROP_COND']['COND'] == 'EQUAL' || $this->arSKUExport['SKU_PROP_COND']['COND'] == 'NONEQUAL')
192 $mxValues = $this->arSKUExport['SKU_PROP_COND']['VALUES'];
193
194 $arOfferFilter[$strExportKey] = $mxValues;
195 }
196
197 $arOfferFilter['PROPERTY_'.$this->arOffers['SKU_PROPERTY_ID']] = $itemId;
198 return \CIBlockElement::GetList(array(),$arOfferFilter,false,false,$arOfferSelect);
199 }
200
201 protected function getProperties($arItem)
202 {
203 $arCross = array();
204
205 if (!empty($arItem['PROPERTIES']))
206 {
207 foreach ($arItem['PROPERTIES'] as &$arProp)
208 $arCross[$arProp['ID']] = $arProp;
209
210 if (isset($arProp))
211 unset($arProp);
212 }
213
214 return $arCross;
215 }
216
217 protected function getItemParams(array $itemOffer)
218 {
219 $itemOffer["QUANTITY"] = $this->getQuantity($itemOffer["ID"]);
220 $itemOffer["CATEGORIES"] = $this->getCategories($itemOffer["ID"]);
221 $itemOffer["DETAIL_PICTURE_URL"] = $this->getPictureUrl((int)$itemOffer["DETAIL_PICTURE"]);
222 $itemOffer["PREVIEW_PICTURE_URL"] = $this->getPictureUrl((int)$itemOffer["PREVIEW_PICTURE"]);
223 $itemOffer["PARAMS"] = $this->getParams($itemOffer, $this->arOfferIBlock);
224 $itemOffer["DETAIL_PAGE_URL"] = $this->getDetailPageUrl($itemOffer["~DETAIL_PAGE_URL"]);
225
226 return $itemOffer;
227 }
228
234 protected function getItemProps($obOfferItem, array $arItem)
235 {
236 $arCross = (!empty($arItem['PROPERTIES']) ? $arItem['PROPERTIES'] : array());
237 $props = $obOfferItem->GetProperties();
238
239 if (!empty($props))
240 foreach ($props as $arProp)
241 $arCross[$arProp['ID']] = $arProp;
242
243 return $arCross;
244 }
245
251 protected function getMinPriceOffer($rsOfferItems, $arItem)
252 {
253 $dblAllMinPrice = 0;
254 $boolFirst = true;
255
256 while ($obOfferItem = $rsOfferItems->GetNextElement())
257 {
258 $arOfferItem = $obOfferItem->GetFields();
259 $arOfferItem["PRICES"] = $this->getPrices($obOfferItem["ID"], $this->arOfferIBlock['LID']);
260
261 if ($arOfferItem["PRICES"]["MIN"] <= 0)
262 continue;
263
264 if ($boolFirst)
265 {
266 $dblAllMinPrice = $arOfferItem["PRICES"]["MIN"];
267 $boolFirst = false;
268 }
269 else
270 {
271 if ($dblAllMinPrice > $arOfferItem["PRICES"]["MIN_RUB"])
272 $dblAllMinPrice = $arOfferItem["PRICES"]["MIN_RUB"];
273 else
274 continue;
275 }
276 }
277
278 $arOfferItem['PROPERTIES'] = $this->getItemProps($obOfferItem, $arItem);
279 $arCurrentOffer = $arOfferItem;
280
281 if (!empty($arCurrentOffer) && $arCurrentOffer["PRICES"]["MIN"] > 0)
282 {
283 $arOfferItem = $arCurrentOffer;
284 $arOfferItem = $this->getItemParams($arOfferItem);
285 }
286 else
287 {
288 $arOfferItem = array();
289 }
290
291 return $arOfferItem;
292 }
293
294 protected function nextItem()
295 {
297 if(!$obItem = $this->dbItems->GetNextElement())
298 return false;
299 $arItem = $obItem->GetFields();
300
301 $arItem['PROPERTIES'] = $obItem->GetProperties($arItem);
302 $arItem["CATEGORIES"] = $this->getCategories($arItem["ID"]);
303 $arItem["DETAIL_PICTURE_URL"] = $this->getPictureUrl((int)$arItem["DETAIL_PICTURE"]);
304 $arItem["PREVIEW_PICTURE_URL"] = $this->getPictureUrl((int)$arItem["PREVIEW_PICTURE"]);
305 $arItem['OFFERS'] = array();
306 $arItem['SELECT_OFFER_PROPS'] = $this->arSelectOfferProps;
307
308 $strOfferTemplateURL = $this->getOfferTemplateUrl();
309 $rsOfferItems = $this->getOffersItemsDb($arItem["ID"]);
310
311 if (!empty($strOfferTemplateURL))
312 $rsOfferItems->SetUrlTemplates($strOfferTemplateURL);
313
314 if (YANDEX_SKU_EXPORT_MIN_PRICE == $this->arSKUExport['SKU_EXPORT_COND'])
315 {
316 $arOfferItem = $this->getMinPriceOffer($rsOfferItems, $arItem);
317
318 if(!empty($arOfferItem))
319 {
320 $arOfferItem = $this->getItemParams($arOfferItem);
321 $arItem['OFFERS'][] = $arOfferItem;
322 }
323 }
324 else
325 {
326 while ($obOfferItem = $rsOfferItems->GetNextElement())
327 {
328 $arOfferItem = $obOfferItem->GetFields();
329 $arOfferItem["PRICES"] = $this->getPrices($arOfferItem["ID"], $this->arOfferIBlock['LID']);
330
331 if ($arOfferItem["PRICES"]["MIN"] <= 0)
332 continue;
333
334 $arOfferItem['PROPERTIES'] = $this->getItemProps($obOfferItem, $arItem);
335 $arOfferItem = $this->getItemParams($arOfferItem);
336 $arItem['OFFERS'][] = $arOfferItem;
337 }
338 }
339
340 if(empty($arItem['OFFERS']) && $this->catalogType == \CCatalogSku::TYPE_FULL)
341 {
342 $arItem["QUANTITY"] = $this->getQuantity($arItem["ID"]);
343 $arItem["PRICES"] = $this->getPrices($arItem["ID"], $this->arIblock['LID']);
344 $arItem["PARAMS"] = $this->getParams($arItem, $this->arIblock);
345 $arItem["DETAIL_PAGE_URL"] = $this->getDetailPageUrl($arItem["~DETAIL_PAGE_URL"]);
346 }
347
348 return $arItem;
349 }
350}
getParams($product, $arIblock)
getPrices($productId, $siteId)
getItemProps($obOfferItem, array $arItem)
getMinPriceOffer($rsOfferItems, $arItem)
__construct($catalogType, $params)