Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
deletebasketitemaction.php
1<?php
2
4
7
15{
16 public function run(int $id)
17 {
18 $deleteBasketItemResult = $this->deleteBasketItem($id);
19 if (!$deleteBasketItemResult->isSuccess())
20 {
21 $this->addErrors($deleteBasketItemResult->getErrors());
22 }
23 }
24
25 public function deleteBasketItem(int $id): Sale\Result
26 {
27 $result = new Sale\Result();
28
29 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
30
32 $basketClass = $registry->getBasketClassName();
33
34 $basketIterator = $basketClass::getList([
35 'select' => ['ORDER_ID', 'FUSER_ID', 'LID'],
36 'filter' => [
37 '=ID' => $id,
38 ]
39 ]);
40 if ($basketItemData = $basketIterator->fetch())
41 {
42 if (empty($basketItemData['ORDER_ID']))
43 {
44 $basket = $basketClass::loadItemsForFUser($basketItemData['FUSER_ID'], $basketItemData['LID']);
45 if ($basket && !$basket->isEmpty())
46 {
47 $basket->getItemByBasketCode($id)->delete();
48
49 $saveResult = $basket->save();
50 if ($saveResult->isSuccess())
51 {
52 $result->setData(['basket' => $basket]);
53 }
54 else
55 {
57 foreach ($saveResult->getErrors() as $error)
58 {
59 // save basket error
60 $result->addError(
61 new Main\Error(
62 $error->getMessage(),
64 )
65 );
66 }
67 }
68 }
69 else
70 {
71 $result->addError(
72 new Main\Error(
73 'basket load error',
75 )
76 );
77 }
78 }
79 else
80 {
81 $result->addError(
82 new Main\Error(
83 'there is order with this basket item',
85 )
86 );
87 }
88 }
89 else
90 {
91 $result->addError(
92 new Main\Error(
93 'basket item with id '.$id.' is not exists',
95 )
96 );
97 }
98
99 return $result;
100 }
101}
addErrors(array $errors)
Definition action.php:213