Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
product.php
1<?php
2
4
8
9class Product extends DataConverter
10{
11 protected $ebayCategories;
12 protected $attributesList;
13 protected $attributesItem;
16 protected $siteId;
17
18 public function __construct($params)
19 {
20 if(!isset($params["SITE_ID"]) || $params["SITE_ID"] == '')
21 throw new ArgumentNullException("SITE_ID");
22
23 $this->siteId = $params["SITE_ID"];
24
25 if(!\Bitrix\Main\Loader::includeModule('iblock'))
26 throw new SystemException("Can't include module \"iblock\"!");
27 }
28
29 public function convert($data)
30 {
31 $this->ebayCategories = $this->bitrixToEbayCategories($data["IBLOCK_ID"], $data["CATEGORIES"]);
32
33 if(empty($this->ebayCategories))
34 throw new SystemException('Can\'t recieve categories for ebay. Product id: '.$data["ID"].', product categories ids: '.implode(', '.$data["CATEGORIES"]));
35
36 $this->attributesList = $this->getAttributesList($data["IBLOCK_ID"], $this->ebayCategories);
37 $this->attributesItem = $this->getAttributesItem($this->attributesList, $data);
38 $this->bitrixCategories = $data["CATEGORIES"];
39
40 if(isset($data["OFFERS"]) && is_array($data["OFFERS"]) && !empty($data["OFFERS"]))
41 {
42 $result = $this->getItemDataOffers($data);
43
44 foreach($data["OFFERS"] as $offer)
45 $result .= $this->getItemDataOffersOffer($offer, $data["IBLOCK_ID"]."_".$data["ID"]);
46 }
47 else
48 {
49 $result = $this->getItemData($data);
50 }
51
52 return $result;
53 }
54
55 protected function getItemDataOffersOffer($data, $parentSKU)
56 {
57 $attributies = $this->getAttributesItem($this->variationsVector, $data);
58
59 $result = "\t<Listing>\n";
60 $result .= "\t\t<Product>\n";
61 $result .= "\t\t\t<SKU VariantOf=\"".$parentSKU."\">".$parentSKU."_".$data["ID"]."</SKU>\n";
62 $result .= "\t\t\t<ProductInformation>\n";
63
64 if(!empty($attributies))
65 {
66 $result .= "\t\t\t<Attributes>\n";
67
68 foreach($attributies as $attrName => $attrValue)
69 $result .= "\t\t\t\t<Attribute Name=\"".\CDataXML::xmlspecialchars($attrName)."\">".\CDataXML::xmlspecialchars($attrValue)."</Attribute>\n";
70
71 $result .= "\t\t\t</Attributes>\n";
72 }
73
74 if($data["DETAIL_PICTURE_URL"] <> '' || $data["PREVIEW_PICTURE_URL"] <> '')
75 {
76 $result .= "\t\t\t<PictureUrls>\n";
77 $result .= "\t\t\t\t<PictureUrl>".($data["DETAIL_PICTURE_URL"] <> '' ? $data["DETAIL_PICTURE_URL"] : $data["PREVIEW_PICTURE_URL"] )."</PictureUrl>\n";
78 $result .= "\t\t\t</PictureUrls>\n";
79 }
80
81 $result .= "\t\t\t</ProductInformation>\n";
82 $result .= "\t\t</Product>\n";
83 $result .= "\t</Listing>\n";
84 return $result;
85 }
86
87 protected function getItemDataOffers($data)
88 {
89 $this->variationsVector = array_diff_key($this->attributesList, $this->attributesItem);
90
91 $result = "\t<Listing>\n";
92 $result .= "\t\t<ProductVariationGroup>\n";
93 $result .= "\t\t\t<Country>RU</Country>\n";
94 $result .= "\t\t\t<GroupId>".$data["IBLOCK_ID"]."_".$data["ID"]."</GroupId>\n";
95
96 if(is_array($this->variationsVector) && !empty($this->variationsVector))
97 {
98 $result .= "\t\t\t<VariationVector>\n";
99
100 foreach($this->variationsVector as $ebayAttributeName => $bitrixPropId)
101 $result .= "\t\t\t\t<Name>".\CDataXML::xmlspecialchars($ebayAttributeName)."</Name>\n";
102
103 $result .= "\t\t\t</VariationVector>\n";
104 }
105
106 $result .= "\t\t\t<Categories>\n";
107
108 foreach($this->ebayCategories as $category)
109 $result .= "\t\t\t\t<Category Type=\"eBayLeafCategory\">".$category."</Category>\n";
110
111 $result .= "\t\t\t</Categories>\n";
112 $result .= "\t\t\t<SharedProductInformation>\n";
113 $result .= "\t\t\t<Title>".\CDataXML::xmlspecialchars($data["NAME"])."</Title>\n";
114 $result .= "\t\t\t<Description>\n";
115 $result .= "\t\t\t\t<ProductDescription>\n";
116 $result .= "<![CDATA[\n";
117 $result .= $data["~PREVIEW_TEXT"] <> '' ? $data["~PREVIEW_TEXT"] : $data["~DETAIL_TEXT"]."\n";
118 $result .= "]]>\n";
119 $result .= "</ProductDescription>\n";
120 $result .= "\t\t\t</Description>\n";
121
122 if(!empty($this->attributesItem))
123 {
124 $result .= "\t\t\t<Attributes>\n";
125
126 foreach($this->attributesItem as $attrName => $attrValue)
127 {
128 if(is_array($attrValue))
129 {
130 $attrValue = current($attrValue);
131 }
132
133 $result .= "\t\t\t\t<Attribute Name=\"".\CDataXML::xmlspecialchars($attrName)."\">".\CDataXML::xmlspecialchars($attrValue)."</Attribute>\n";
134 }
135
136 $result .= "\t\t\t</Attributes>\n";
137 }
138
139 $result .= "\t\t\t<PictureUrls>\n";
140 $result .= "\t\t\t\t<PictureUrl>".($data["DETAIL_PICTURE_URL"] <> '' ? $data["DETAIL_PICTURE_URL"] : $data["PREVIEW_PICTURE_URL"] )."</PictureUrl>\n";
141 $result .= "\t\t\t</PictureUrls>\n";
142 $result .= "<ConditionInfo>
143 <Condition>NEW</Condition>
144 </ConditionInfo>";
145 $result .= "\t\t\t</SharedProductInformation>\n";
146 reset($this->ebayCategories);
147 $result .= $this->getListingDetails($data["IBLOCK_ID"], current($this->ebayCategories));
148 $result .= "\t\t</ProductVariationGroup>\n";
149 $result .= "\t</Listing>\n";
150 return $result;
151 }
152
153 protected function getEbayCategoryAttrName($ebeyAttributeId)
154 {
155 $res = CategoryVariationTable::getById($ebeyAttributeId);
156
157 if($category = $res->fetch())
158 $result = $category["NAME"];
159 else
160 $result = "";
161
162 return $result;
163 }
164
165 protected function getAttributesItem($attributesList, $data)
166 {
167 if(!is_array($data["PROPERTIES"]))
168 return array();
169
170 $result = array();
171
172 foreach($attributesList as $ebayCategoryAttrId => $bitrixAttr)
173 {
174 $value = $this->getBitrixItemPropValue($bitrixAttr, $data["PROPERTIES"]);
175 $name = $this->getEbayCategoryAttrName($ebayCategoryAttrId);
176
177 if($value !== false)
178 $result[$name] = $value;
179 }
180
181 return $result;
182 }
183
184 protected function getItemData($data)
185 {
186 $result = "\t<Listing>\n";
187 $result .= "\t\t<Product>\n";
188 $result .= "\t\t\t<SKU>".$data["IBLOCK_ID"]."_".$data["ID"]."</SKU>\n";
189 $result .= "\t\t\t<ProductInformation>\n";
190 $result .= "\t\t\t\t<Country>RU</Country>\n";
191 $result .= "\t\t\t\t<Title>".\CDataXML::xmlspecialchars($data["NAME"])."</Title>\n";
192 $result .= "\t\t\t\t<Description>\n";
193 $result .= "\t\t\t\t\t<ProductDescription>\n";
194 $result .= "<![CDATA[\n";
195 $result .= $data["~PREVIEW_TEXT"] <> '' ? $data["~PREVIEW_TEXT"] : $data["~DETAIL_TEXT"]."\n";
196 $result .= "]]>\n";
197 $result .= "</ProductDescription>\n";
198 $result .= "\t\t\t\t</Description>\n";
199 $result .= "<ConditionInfo>
200 <Condition>NEW</Condition>
201 </ConditionInfo>";
202
203 if(!empty($this->attributesItem))
204 {
205 $result .= "\t\t\t\t<Attributes>\n";
206
207 foreach($this->attributesItem as $attrName => $attrValue)
208 {
209 if(is_array($attrValue))
210 {
211 $attrValue = current($attrValue);
212 }
213
214 $result .= "\t\t\t\t\t<Attribute Name=\"".\CDataXML::xmlspecialchars($attrName)."\">".\CDataXML::xmlspecialchars($attrValue)."</Attribute>\n";
215 }
216
217 $result .= "\t\t\t\t</Attributes>\n";
218 }
219
220 $result .= "\t\t\t\t<PictureUrls>\n";
221 $result .= "\t\t\t\t\t<PictureUrl>".($data["DETAIL_PICTURE_URL"] <> '' ? $data["DETAIL_PICTURE_URL"] : $data["PREVIEW_PICTURE_URL"] )."</PictureUrl>\n";
222 $result .= "\t\t\t\t</PictureUrls>\n";
223 $result .= "\t\t\t\t<Categories>\n";
224
225 foreach($this->ebayCategories as $category)
226 $result .= "\t\t\t\t\t<Category Type=\"eBayLeafCategory\">".$category."</Category>\n";
227
228 $result .= "\t\t\t\t</Categories>\n";
229 $result .= "\t\t\t</ProductInformation>\n";
230 $result .= "\t\t</Product>\n";
231 reset($this->ebayCategories);
232 $result .= $this->getListingDetails($data["IBLOCK_ID"], current($this->ebayCategories));
233 $result .= "\t</Listing>\n";
234 return $result;
235 }
236
237 protected function getListingDetails($iBlockId, $ebayCategory)
238 {
239 $policy = $this->getPolicyForCategory($iBlockId, $ebayCategory);
240 $result = "\t\t<ListingDetails>\n";
241
242 if(!empty($policy["RETURN"]))
243 $result .= "\t\t\t<ReturnPolicy>".\CDataXML::xmlspecialchars($policy["RETURN"])."</ReturnPolicy>\n";
244
245 if(!empty($policy["SHIPPING"]))
246 $result .= "\t\t\t<ShippingPolicy>".\CDataXML::xmlspecialchars($policy["SHIPPING"])."</ShippingPolicy>\n";
247
248 if(!empty($policy["PAYMENT"]))
249 $result .= "\t\t\t<PaymentPolicy>".\CDataXML::xmlspecialchars($policy["PAYMENT"])."</PaymentPolicy>\n";
250
251 $result .= "\t\t</ListingDetails>\n";
252 return $result;
253 }
254
255 protected function getBitrixItemPropValue($propId, array $props)
256 {
257 $result = false;
258
259 foreach($props as $property)
260 {
261 if($property["ID"] == $propId)
262 {
263 $result = $property["~VALUE"];
264 break;
265 }
266 }
267
268 return $result;
269 }
270
271 protected function getAttributesList($iblockId, array $ebayCategories)
272 {
273 $result = array();
274
275 foreach($ebayCategories as $category)
276 {
277 $mapEntityId = \Bitrix\Sale\TradingPlatform\Ebay\MapHelper::getCategoryVariationEntityId($iblockId, $category);
278
279 $catMapVarRes = \Bitrix\Sale\TradingPlatform\MapTable::getList(array(
280 "filter" => array(
281 "ENTITY_ID" => $mapEntityId
282 )
283 ));
284
285 while($arMapRes = $catMapVarRes->fetch())
286 $result[$arMapRes["VALUE_EXTERNAL"]] = $arMapRes["VALUE_INTERNAL"];
287 }
288
289 return $result;
290 }
291
292 /* note: limitation for Russia - product can be just in one category */
293 protected function bitrixToEbayCategories($iblockId, array $bitrixCategories)
294 {
295 $categories = $this->getEbayCategoriesParams($iblockId, $bitrixCategories);
296 $result = array();
297
298 foreach($categories as $category)
299 $result[] = $category["VALUE_EXTERNAL"];
300
301 return $result;
302 }
303
304 protected static function getEbayCategoriesParams($iblockId, array $bitrixCategories = [])
305 {
306 static $entitiesIds = [];
307
308 if (empty($entitiesIds[$iblockId]))
309 {
310 $res = \Bitrix\Sale\TradingPlatform\Ebay\MapHelper::getCategoryEntityId($iblockId);
311
312 if (!$res)
313 {
314 return [];
315 }
316
317 $entitiesIds[$iblockId] = $res;
318 }
319
320 static $params = array();
321
322 if (!isset($params[$iblockId]))
323 {
324 $params[$iblockId] = [];
325
326 $catRes = \Bitrix\Sale\TradingPlatform\MapTable::getList([
327 'filter' => [
328 '=ENTITY_ID' => $entitiesIds[$iblockId],
329 ],
330 ]);
331
332 while($category = $catRes->fetch())
333 {
334 if ((int)$category["VALUE_INTERNAL"] > 0)
335 {
336 $params[$iblockId][$category["VALUE_INTERNAL"]] = $category;
337 }
338 }
339 unset($catRes);
340 }
341
342 $result = array();
343
344 if (!empty($bitrixCategories))
345 {
346 foreach ($bitrixCategories as $catId)
347 {
348 if (isset($params[$iblockId][$catId]) && is_array($params[$iblockId][$catId]))
349 {
350 $result[] = $params[$iblockId][$catId];
351 }
352 else
353 {
354 $res = \CIBlockSection::GetNavChain($iblockId, $catId, ['ID'], true);
355 foreach ($res as $row)
356 {
357 $id = (int)$row['ID'];
358 if (isset($params[$iblockId][$id]) && is_array($params[$iblockId][$id]))
359 {
360 $result[] = $params[$iblockId][$id];
361 }
362 }
363 }
364 }
365 }
366 else
367 {
368 $result = $params[$iblockId];
369 }
370
371 return $result;
372 }
373
374 protected function getPolicyForCategory($iblockId, $ebayCategory)
375 {
376 static $result = array();
377
378 if(!isset($result[$ebayCategory]))
379 {
380 $ebay = \Bitrix\Sale\TradingPlatform\Ebay\Ebay::getInstance();
381 $settings = $ebay->getSettings();
382 $siteSettings = $settings[$this->siteId];
383 $policyReturnId = "";
384 $policyPaymentId = "";
385 $policyShippingId = "";
386 $result[$ebayCategory] = array();
387
388 foreach($this->getEbayCategoriesParams($iblockId) as $categoryParams)
389 {
390 if($categoryParams["VALUE_EXTERNAL"] != $ebayCategory)
391 continue;
392
393 if(!empty($categoryParams["PARAMS"]["POLICY"]))
394 {
395 if(!empty($categoryParams["PARAMS"]["POLICY"]["RETURN"]))
396 $policyReturnId = $categoryParams["PARAMS"]["POLICY"]["RETURN"];
397
398 if(!empty($categoryParams["PARAMS"]["POLICY"]["SHIPPING"]))
399 $policyShippingId = $categoryParams["PARAMS"]["POLICY"]["SHIPPING"];
400
401 if(!empty($categoryParams["PARAMS"]["POLICY"]["PAYMENT"]))
402 $policyPaymentId = $categoryParams["PARAMS"]["POLICY"]["PAYMENT"];
403 }
404
405 if($policyReturnId == '' && !empty($siteSettings["POLICY"]["RETURN"]["DEFAULT"]))
406 $policyReturnId = $siteSettings["POLICY"]["RETURN"]["DEFAULT"];
407
408 if($policyShippingId == '' && !empty($siteSettings["POLICY"]["SHIPPING"]["DEFAULT"]))
409 $policyShippingId = $siteSettings["POLICY"]["SHIPPING"]["DEFAULT"];
410
411 if($policyPaymentId == '' && !empty($siteSettings["POLICY"]["PAYMENT"]["DEFAULT"]))
412 $policyPaymentId = $siteSettings["POLICY"]["PAYMENT"]["DEFAULT"];
413
414 if($policyReturnId != "" && !empty($siteSettings["POLICY"]["RETURN"]["LIST"][$policyReturnId]))
415 $result[$ebayCategory]["RETURN"] = $siteSettings["POLICY"]["RETURN"]["LIST"][$policyReturnId];
416
417 if($policyShippingId != "" && !empty($siteSettings["POLICY"]["SHIPPING"]["LIST"][$policyShippingId]))
418 $result[$ebayCategory]["SHIPPING"] = $siteSettings["POLICY"]["SHIPPING"]["LIST"][$policyShippingId];
419
420 if($policyPaymentId != "" && !empty($siteSettings["POLICY"]["PAYMENT"]["LIST"][$policyPaymentId]))
421 $result[$ebayCategory]["PAYMENT"] = $siteSettings["POLICY"]["PAYMENT"]["LIST"][$policyPaymentId];
422
423 break;
424 }
425 }
426
427 return $result[$ebayCategory];
428 }
429}
bitrixToEbayCategories($iblockId, array $bitrixCategories)
Definition product.php:293
static getEbayCategoriesParams($iblockId, array $bitrixCategories=[])
Definition product.php:304