17 public function run(
int $id, array $fields)
21 $updateBasketItemResult = $this->updateBasketItem($id, $fields);
22 if (!$updateBasketItemResult->isSuccess())
24 $this->
addErrors($updateBasketItemResult->getErrors());
28 $updateBasketItemData = $updateBasketItemResult->getData();
30 $basketItem = $updateBasketItemData[
'basketItem'];
31 return Sale\Helpers\Controller\Action\Entity\Order::getOrderProductByBasketItem($basketItem);
34 public function updateBasketItem(
int $id, array $fields):
Sale\Result
41 $basketClass = $registry->getBasketClassName();
43 $basketIterator = $basketClass::getList([
44 'select' => [
'ORDER_ID',
'FUSER_ID',
'LID'],
50 $fields = $this->prepareBasketFields($fields);
52 if ($basketItemData = $basketIterator->fetch())
54 if (empty($basketItemData[
'ORDER_ID']))
56 $basket = $this->getBasket($basketItemData[
'FUSER_ID'], $basketItemData[
'LID']);
57 if ($basket && !$basket->isEmpty())
59 $basketItem = $basket->getItemByBasketCode($id);
60 foreach ($fields as $fieldName => $fieldValue)
62 $setFieldResult = $basketItem->setField($fieldName, $fieldValue);
63 if (!$setFieldResult->isSuccess())
66 foreach ($setFieldResult->getErrors() as $error)
78 $checkQuantityResult = $this->checkQuantity($basket);
79 if (!$checkQuantityResult->isSuccess())
81 foreach ($checkQuantityResult->getErrors() as $error)
92 if ($result->isSuccess())
94 $saveResult = $basket->save();
95 if ($saveResult->isSuccess())
99 'basketItem' => $basketItem,
105 foreach ($saveResult->getErrors() as $error)
109 $error->getMessage(),
121 'basket item load error',
131 'there is order with this basket item',
141 'basket item with id '.$id.
' is not exists',
150 private function getBasket(
int $fuserId,
string $siteId):
Sale\Basket
153 $basketStorage = Sale\Basket\Storage::getInstance($fuserId, $siteId);
154 return $basketStorage->getBasket();
161 $actualQuantityList = $this->getActualQuantityList($basket);
162 Sale\BasketComponentHelper::correctQuantityRatio($basket);
164 $updatedQuantityList = $this->getActualQuantityList($basket);
165 foreach ($updatedQuantityList as $basketCode => $itemQuantity)
167 if (!isset($actualQuantityList[$basketCode]) || $itemQuantity !== $actualQuantityList[$basketCode])
169 $result->addError(
new Main\
Error(
'quantity is incorrect'));
176 private function getActualQuantityList(
Sale\
Basket $basket): array
180 if (!$basket->isEmpty())
183 foreach ($basket as $basketItem)
185 if ($basketItem->canBuy() && !$basketItem->isDelay())
187 $quantityList[$basketItem->getBasketCode()] = $basketItem->getQuantity();
192 return $quantityList;
195 private function prepareBasketFields(array $fields): array
197 $fields = $this->filterBasketFieldsOnUpdate($fields);
199 $fields[
'PRODUCT_PROVIDER_CLASS'] = Catalog\Product\Basket::getDefaultProviderName();
201 $settableFields = Sale\BasketItemBase::getSettableFields();
204 static function ($field) use ($settableFields) {
205 return in_array($field, $settableFields,
true);
211 private function filterBasketFieldsOnUpdate(array $basketFields): array