Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
addbasketitemaction.php
1<?php
2
4
8
16{
17 private function checkParams(array $fields): Sale\Result
18 {
19 $result = new Sale\Result();
20
21 if (empty($fields['SITE_ID']))
22 {
23 $result->addError(
24 new Main\Error(
25 'siteId not found',
27 )
28 );
29 }
30
31 if (empty($fields['FUSER_ID']) || (int)$fields['FUSER_ID'] <= 0)
32 {
33 $result->addError(
34 new Main\Error(
35 'fuserId not found',
37 )
38 );
39 }
40
41 if (empty($fields['PRODUCT']))
42 {
43 $result->addError(
44 new Main\Error(
45 'product not found',
47 )
48 );
49 }
50
51 return $result;
52 }
53
54 public function run(array $fields)
55 {
56 $result = [];
57
58 $addBasketItemResult = $this->addBasketItem($fields);
59 if (!$addBasketItemResult->isSuccess())
60 {
61 $this->addErrors($addBasketItemResult->getErrors());
62 return $result;
63 }
64
65 $addBasketItemData = $addBasketItemResult->getData();
67 $basketItem = $addBasketItemData['basketItem'];
68 return Sale\Helpers\Controller\Action\Entity\Order::getOrderProductByBasketItem($basketItem);
69 }
70
71 public function addBasketItem(array $fields): Sale\Result
72 {
73 $result = new Sale\Result();
74
75 $checkParamsResult = $this->checkParams($fields);
76 if (!$checkParamsResult->isSuccess())
77 {
78 $result->addErrors($checkParamsResult->getErrors());
79 return $result;
80 }
81
82 $fuserId = $fields['FUSER_ID'];
83 $siteId = $fields['SITE_ID'];
84 $product = $fields['PRODUCT'];
85 $options = [
86 'USE_MERGE' => !isset($fields['USE_MERGE']) || $fields['USE_MERGE'] !== 'N' ? 'Y' : 'N',
87 ];
88
89 $basket = $this->getBasketByFuserId($fuserId, $siteId);
90
91 $product = $this->prepareBasketFields($product);
92
93 $addProductToBasketResult = Catalog\Product\Basket::addProductToBasket($basket, $product, ['SITE_ID' => $siteId], $options);
94 if ($addProductToBasketResult->isSuccess())
95 {
96 $saveBasketResult = $basket->save();
97 if ($saveBasketResult->isSuccess())
98 {
99 $addProductToBasketData = $addProductToBasketResult->getData();
100 if ($basketItem = $addProductToBasketData['BASKET_ITEM'])
101 {
102 $result->setData([
103 'basket' => $basket,
104 'basketItem' => $basketItem,
105 ]);
106 }
107 }
108 else
109 {
111 foreach ($saveBasketResult->getErrors() as $error)
112 {
113 // save basket error
114 $result->addError(
115 new Main\Error(
116 $error->getMessage(),
118 )
119 );
120 }
121 }
122 }
123 else
124 {
126 foreach ($addProductToBasketResult->getErrors() as $error)
127 {
128 $result->addError(
129 new Main\Error(
130 $error->getMessage(),
132 )
133 );
134 }
135 }
136
137 return $result;
138 }
139
140 private function getBasketByFuserId($fuserId, $siteId): Sale\BasketBase
141 {
142 $registry = Sale\Registry::getInstance(Sale\Registry::REGISTRY_TYPE_ORDER);
143
145 $basketClassName = $registry->getBasketClassName();
146 return $basketClassName::loadItemsForFUser($fuserId, $siteId);
147 }
148
149 private function prepareBasketFields(array $fields): array
150 {
151 $fields = $this->filterBasketFieldsOnAdd($fields);
152
153 $fields['MODULE'] = 'catalog';
154 $fields['PRODUCT_PROVIDER_CLASS'] = Catalog\Product\Basket::getDefaultProviderName();
155
156 return $fields;
157 }
158
159 private function filterBasketFieldsOnAdd(array $basketFields): array
160 {
161 return (new Sale\Rest\Entity\BasketItem())->internalizeFieldsAdd($basketFields);
162 }
163}
addErrors(array $errors)
Definition action.php:213