17 private function checkParams(array $fields):
Sale\Result
21 if (empty($fields[
'SITE_ID']))
31 if (empty($fields[
'FUSER_ID']) || (
int)$fields[
'FUSER_ID'] <= 0)
41 if (empty($fields[
'PRODUCT']))
54 public function run(array $fields)
58 $addBasketItemResult = $this->addBasketItem($fields);
59 if (!$addBasketItemResult->isSuccess())
61 $this->
addErrors($addBasketItemResult->getErrors());
65 $addBasketItemData = $addBasketItemResult->getData();
67 $basketItem = $addBasketItemData[
'basketItem'];
68 return Sale\Helpers\Controller\Action\Entity\Order::getOrderProductByBasketItem($basketItem);
71 public function addBasketItem(array $fields):
Sale\Result
75 $checkParamsResult = $this->checkParams($fields);
76 if (!$checkParamsResult->isSuccess())
78 $result->addErrors($checkParamsResult->getErrors());
82 $fuserId = $fields[
'FUSER_ID'];
83 $siteId = $fields[
'SITE_ID'];
84 $product = $fields[
'PRODUCT'];
86 'USE_MERGE' => !isset($fields[
'USE_MERGE']) || $fields[
'USE_MERGE'] !==
'N' ?
'Y' :
'N',
89 $basket = $this->getBasketByFuserId($fuserId, $siteId);
91 $product = $this->prepareBasketFields($product);
93 $addProductToBasketResult = Catalog\Product\Basket::addProductToBasket($basket, $product, [
'SITE_ID' => $siteId], $options);
94 if ($addProductToBasketResult->isSuccess())
96 $saveBasketResult = $basket->save();
97 if ($saveBasketResult->isSuccess())
99 $addProductToBasketData = $addProductToBasketResult->getData();
100 if ($basketItem = $addProductToBasketData[
'BASKET_ITEM'])
104 'basketItem' => $basketItem,
111 foreach ($saveBasketResult->getErrors() as $error)
116 $error->getMessage(),
126 foreach ($addProductToBasketResult->getErrors() as $error)
130 $error->getMessage(),
140 private function getBasketByFuserId($fuserId, $siteId):
Sale\BasketBase
145 $basketClassName = $registry->getBasketClassName();
146 return $basketClassName::loadItemsForFUser($fuserId, $siteId);
149 private function prepareBasketFields(array $fields): array
151 $fields = $this->filterBasketFieldsOnAdd($fields);
153 $fields[
'MODULE'] =
'catalog';
154 $fields[
'PRODUCT_PROVIDER_CLASS'] = Catalog\Product\Basket::getDefaultProviderName();
159 private function filterBasketFieldsOnAdd(array $basketFields): array