Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
catalogprovidercompatibility.php
1<?php
3
5
12{
18 public static function getProductData(array $basketItemData)
19 {
20 $context = array();
21
22 if (!empty($basketItemData['SITE_ID']))
23 {
24 $context['SITE_ID'] = $basketItemData['SITE_ID'];
25 }
26
27 if (!empty($basketItemData['USER_ID']))
28 {
29 $context['USER_ID'] = $basketItemData['USER_ID'];
30 }
31
32 $providerClass = static::getProviderClass($context);
33 if (!$providerClass)
34 {
35 return false;
36 }
37
38 $productId = $basketItemData['PRODUCT_ID'];
39 $transfer = Sale\Internals\TransferProvider::create($providerClass, $context);
40
41 $products = array(
42 $productId => array(
43 'ITEM_CODE' => $productId,
44 'QUANTITY_LIST' => array(
45 $basketItemData['BASKET_ID'] => $basketItemData['QUANTITY'])
46 ,
47 ));
48
49 if (!empty($basketItemData['BASKET_ID']))
50 {
51 $products[$productId]['BASKET_ID'] = $basketItemData['BASKET_ID'];
52 }
53
54 $r = $transfer->getProductData($products);
55
56 if ($r->isSuccess())
57 {
58 $data = $r->getData();
59 if (!empty($data['PRODUCT_DATA_LIST']))
60 {
61 $productDataList = $data['PRODUCT_DATA_LIST'];
62 if (isset($productDataList[$productId]))
63 {
64 $productData = $productDataList[$productId];
65
66 if (!empty($productData['PRICE_LIST']))
67 {
68 $basketItemCode = $basketItemData['BASKET_ID'];
69
70 if (!empty($productData['PRICE_LIST'][$basketItemCode]))
71 {
72 $priceData = $productData['PRICE_LIST'][$basketItemCode];
73
74 if (!isset($priceData['QUANTITY']) && isset($priceData['AVAILABLE_QUANTITY']))
75 {
76 $priceData['QUANTITY'] = $priceData['AVAILABLE_QUANTITY'];
77 }
78
79 $productData = $priceData + $productData;
80 }
81 }
82
83 return $productData;
84 }
85 }
86 }
87
88 return false;
89 }
90
96 public static function orderProduct(array $basketItemData)
97 {
98 return static::getProductData($basketItemData);
99 }
100
106 public static function getSetItems($productId)
107 {
108 $resultList = array();
109
110 $providerClass = static::getProviderClass();
111 if (!$providerClass)
112 {
113 return false;
114 }
115
116 $transfer = Sale\Internals\TransferProvider::create($providerClass, static::getContext());
117
118 $products = array($productId => array());
119 $r = $transfer->getBundleItems($products);
120
121 if ($r->isSuccess())
122 {
123 $data = $r->getData();
124 if (!empty($data['BUNDLE_LIST']))
125 {
126 $resultList = $data['BUNDLE_LIST'];
127 }
128 }
129
130 return $resultList;
131 }
132
138 public static function reserveProduct(array $values)
139 {
140 $providerClass = static::getProviderClass();
141 if (!$providerClass)
142 {
143 return false;
144 }
145
146 $productId = $values['PRODUCT_ID'];
147 $transfer = Sale\Internals\TransferProvider::create($providerClass, static::getContext());
148
149 $products = array(
150 $productId => array(
151 'ITEM_CODE' => $productId,
152 'QUANTITY' => $values['QUANTITY'] * ($values['UNDO_RESERVATION'] == 'Y'? -1 : 1),
153 ));
154
155 $r = $transfer->reserve($products);
156 if ($r->isSuccess())
157 {
158 $data = $r->getData();
159 if (!empty($data))
160 {
161 $result = new Sale\Result();
162
163 $result->setData(array(
164 'RESERVED_PRODUCTS_LIST' => array(
165 $productId => $data
166 )
167 ));
168
169 $r = $transfer->setItemsResultAfterReserve($products, $result);
170 }
171 }
172
173 return false;
174 }
175
181 public static function deductProduct(array $values)
182 {
183 $providerClass = static::getProviderClass();
184 if (!$providerClass)
185 {
186 return false;
187 }
188
189 $productId = $values['PRODUCT_ID'];
190 $transfer = Sale\Internals\TransferProvider::create($providerClass, static::getContext());
191
192 $products = array(
193 $productId => array(
194 'ITEM_CODE' => $productId,
195 'PRODUCT_ID' => $productId,
196 'QUANTITY' => $values['QUANTITY'] * ($values['UNDO_DEDUCTION'] == 'Y'? -1 : 1),
197 ));
198
199 $r = $transfer->ship($products);
200 if ($r->isSuccess())
201 {
202 $data = $r->getData();
203 if (!empty($data))
204 {
205 $result = new Sale\Result();
206
207 $result->setData(array(
208 'SHIPPED_PRODUCTS_LIST' => array(
209 $productId => $data
210 )
211 ));
212
213 $r = $transfer->setItemsResultAfterShip($products, $result);
214 }
215 }
216
217 return false;
218 }
219
225 public static function viewProduct(array $values)
226 {
227 $result = false;
228 $providerClass = static::getProviderClass();
229 if (!$providerClass)
230 {
231 return $result;
232 }
233
234 $productId = $values['PRODUCT_ID'];
235 $transfer = Sale\Internals\TransferProvider::create($providerClass, static::getContext());
236
237 if (empty($values['SITE_ID']))
238 {
239 $values['SITE_ID'] = SITE_ID;
240 }
241
242 $products = array(
243 $productId => array(
244 "PRODUCT_ID" => $productId,
245 "USER_ID" => $values["USER_ID"],
246 "SITE_ID" => $values["SITE_ID"]
247 ));
248
249 $r = $transfer->viewProduct($products);
250 if ($r->isSuccess())
251 {
252 $data = $r->getData();
253
254 if (!empty($data['VIEW_PRODUCTS_LIST']) && array_key_exists($productId, $data['VIEW_PRODUCTS_LIST']))
255 {
256 return $data['VIEW_PRODUCTS_LIST'][$productId];
257 }
258 }
259
260 return $result;
261 }
262
268 public static function getProductStores(array $values)
269 {
270 $result = false;
271 $providerClass = static::getProviderClass();
272 if (!$providerClass)
273 {
274 return $result;
275 }
276
277 $context = static::getContext();
278
279 if (!empty($values['SITE_ID']))
280 {
281 $context['SITE_ID'] = $values['SITE_ID'];
282 }
283
284 $productId = $values['PRODUCT_ID'];
285 $transfer = Sale\Internals\TransferProvider::create($providerClass, $context);
286
287 $products = array(
288 $productId => array(
289 "PRODUCT_ID" => $productId,
290 "BASKET_ID" => $values["BASKET_ID"],
291 "SITE_ID" => $values["SITE_ID"]
292 ));
293
294 $r = $transfer->getProductListStores($products);
295 if ($r->isSuccess())
296 {
297 $data = $r->getData();
298
299 if (!empty($data['PRODUCT_STORES_LIST']) && array_key_exists($productId, $data['PRODUCT_STORES_LIST']))
300 {
301 return $data['PRODUCT_STORES_LIST'][$productId];
302 }
303 }
304
305 return $result;
306 }
307
313 public static function recurringOrderProduct(array $values)
314 {
315 $result = false;
316 $providerClass = static::getProviderClass();
317 if (!$providerClass)
318 {
319 return $result;
320 }
321
322 $context = static::getContext();
323
324 if (!empty($values['SITE_ID']))
325 {
326 $context['SITE_ID'] = $values['SITE_ID'];
327 }
328
329 $productId = $values['PRODUCT_ID'];
330 $transfer = Sale\Internals\TransferProvider::create($providerClass, $context);
331
332 $products = array(
333 $productId => array(
334 "PRODUCT_ID" => $productId,
335 "USER_ID" => $values["USER_ID"],
336 ));
337
338 $r = $transfer->recurring($products);
339 if ($r->isSuccess())
340 {
341 $data = $r->getData();
342
343 if (!empty($data['RECURRING_PRODUCTS_LIST']) && array_key_exists($productId, $data['RECURRING_PRODUCTS_LIST']))
344 {
345 return $data['RECURRING_PRODUCTS_LIST'][$productId];
346 }
347 }
348
349 return $result;
350 }
351
357 public static function getStoresCount(array $values)
358 {
359 $result = false;
360 $context = static::getContext();
361
362 if (isset($values['SITE_ID']))
363 {
364 $context['SITE_ID'] = $values['SITE_ID'];
365 }
366
367 $providerClass = static::getProviderClass($context);
368 if (!$providerClass)
369 {
370 return $result;
371 }
372
373 $transfer = Sale\Internals\TransferProvider::create($providerClass, $context);
374 $r = $transfer->getStoresCount();
375 if ($r->isSuccess())
376 {
377 $data = $r->getData();
378
379 if (isset($data['STORES_COUNT']))
380 {
381 return $data['STORES_COUNT'];
382 }
383 }
384
385 return $result;
386 }
387
388 public static function DeliverProduct(array $values)
389 {
390 $result = false;
391 $providerClass = static::getProviderClass();
392 if (!$providerClass)
393 {
394 return $result;
395 }
396
397 $productId = $values['PRODUCT_ID'];
398 $transfer = Sale\Internals\TransferProvider::create($providerClass, static::getContext());
399
400 if (empty($values['SITE_ID']))
401 {
402 $values['SITE_ID'] = SITE_ID;
403 }
404
405 $products = array(
406 $productId => array(
407 "PRODUCT_ID" => $productId,
408 "USER_ID" => $values["USER_ID"],
409 "ORDER_ID" => $values["ORDER_ID"],
410 "PAID" => $values["PAID"],
411 "BASKET_ID" => $values["BASKET_ID"],
412 ));
413
414 $r = $transfer->deliver($products);
415 if ($r->isSuccess())
416 {
417 $data = $r->getData();
418
419 if (!empty($data['DELIVER_PRODUCTS_LIST']) && array_key_exists($productId, $data['DELIVER_PRODUCTS_LIST']))
420 {
421 return $data['DELIVER_PRODUCTS_LIST'][$productId];
422 }
423 }
424
425 return $result;
426 }
427
433 private static function getProviderClass(array $context = array()): ?Sale\SaleProviderBase
434 {
435 $providerName = Basket::getDefaultProviderName();
436 $providerClass = null;
437
438 $setContext = static::getContext();
439
440 if (!empty($context))
441 {
442 $setContext = $context + $setContext;
443 }
444
445 if (class_exists($providerName))
446 {
447 $providerClass = new $providerName($setContext);
448 }
449
450 return $providerClass;
451 }
452
456 private static function getContext(): array
457 {
458 global $USER;
459
460 return [
461 'SITE_ID' => SITE_ID,
462 'USER_ID' => isset($USER) && $USER instanceof \CUser ? (int)$USER->GetID() : 0,
463 'CURRENCY' => Sale\Internals\SiteCurrencyTable::getSiteCurrency(SITE_ID)
464 ];
465 }
466}