36 private $isLoadForFUserId =
false;
50 isset($this->basketItemIndexMap[
$code])
51 && isset($this->collection[$this->basketItemIndexMap[
$code]])
54 return $this->collection[$this->basketItemIndexMap[
$code]];
57 return parent::getItemByBasketCode(
$code);
73 private static function createBasketObject()
76 $basketClassName = $registry->getBasketClassName();
78 return new $basketClassName;
92 $basket = static::create(
$siteId);
96 $basket->isLoadForFUserId =
true;
99 return $basket->loadFromDb([
116 "ID",
"LID",
"MODULE",
"PRODUCT_ID",
"QUANTITY",
"WEIGHT",
117 "DELAY",
"CAN_BUY",
"PRICE",
"CUSTOM_PRICE",
"BASE_PRICE",
118 'PRODUCT_PRICE_ID',
'PRICE_TYPE_ID',
"CURRENCY",
'BARCODE_MULTI',
119 "RESERVED",
"RESERVE_QUANTITY",
"NAME",
"CATALOG_XML_ID",
120 "VAT_RATE",
"NOTES",
"DISCOUNT_PRICE",
"PRODUCT_PROVIDER_CLASS",
121 "CALLBACK_FUNC",
"ORDER_CALLBACK_FUNC",
"PAY_CALLBACK_FUNC",
122 "CANCEL_CALLBACK_FUNC",
"DIMENSIONS",
"TYPE",
"SET_PARENT_ID",
123 "DETAIL_PAGE_URL",
"FUSER_ID",
'MEASURE_CODE',
'MEASURE_NAME',
124 'ORDER_ID',
'DATE_INSERT',
'DATE_UPDATE',
'PRODUCT_XML_ID',
125 'SUBSCRIBE',
'RECOMMENDATION',
'VAT_INCLUDED',
'SORT',
126 'DATE_REFRESH',
'DISCOUNT_NAME',
'DISCOUNT_VALUE',
'DISCOUNT_COUPON',
127 'XML_ID',
'MARKING_CODE_GROUP'
133 $res = static::getList([
136 "order" => [
'SORT' =>
'ASC',
'ID' =>
'ASC'],
138 while ($item =
$res->fetch())
147 $itemList[$item[
'ID']] = $item;
150 foreach ($itemList as $id => $item)
152 if ($item[
'SET_PARENT_ID'] > 0)
154 $itemList[$item[
'SET_PARENT_ID']][
'ITEMS'][$id] = &$itemList[$id];
159 foreach ($itemList as $id => $item)
161 if ($item[
'SET_PARENT_ID'] == 0)
200 $itemSort = (int)$item->
getField(
'SORT') ?: 100;
202 if ($this->maxItemSort ===
null)
204 $this->maxItemSort = $itemSort;
208 if ($itemSort > $this->maxItemSort)
210 $this->maxItemSort = $itemSort;
214 $this->maxItemSort += 100 + $this->maxItemSort % 100;
229 $basket = static::createBasketObject();
241 public function getPrice()
246 foreach ($this->collection as $basketItem)
248 $orderPrice += $basketItem->getFinalPrice();
260 public function getBasePrice()
265 foreach ($this->collection as $basketItem)
279 public function getVatSum()
284 foreach ($this->collection as $basketItem)
287 if ($basketItem->getQuantity() == 0)
292 $vatSum += $basketItem->getVat();
304 public function getVatRate()
309 foreach ($this->collection as $basketItem)
312 if ($basketItem->getQuantity() == 0)
317 if ($basketItem->getVatRate() > $vatRate)
319 $vatRate = $basketItem->getVatRate();
332 public function verify()
337 foreach ($this->collection as $basketItem)
339 $r = $basketItem->verify();
340 if (!$r->isSuccess())
342 $result->addErrors($r->getErrors());
350 $entityMarker = $registry->getEntityMarkerClassName();
351 $entityMarker::addMarker(
$order, $basketItem, $r);
352 $order->setField(
'MARKED',
'Y');
365 public function getWeight()
370 foreach ($this->collection as $basketItem)
372 $orderWeight += $basketItem->getWeight() * $basketItem->getQuantity();
381 private function getOriginalItemsValues()
396 if ($this->isLoadForFUserId)
425 "select" =>
array(
"ID",
'TYPE',
'SET_PARENT_ID',
'PRODUCT_ID',
'NAME',
'QUANTITY',
'FUSER_ID',
'ORDER_ID'),
430 while ($item =
$dbRes->fetch())
432 if ((
int)$item[
'SET_PARENT_ID'] > 0 && (
int)$item[
'SET_PARENT_ID'] != $item[
'ID'])
459 public function save()
461 $this->checkCallingContext();
465 $this->isSaveExecuting =
true;
471 $r = $this->verify();
472 if (!$r->isSuccess())
474 return $result->addErrors($r->getErrors());
477 $r = $this->callEventOnSaleBasketBeforeSaved();
478 if (!$r->isSuccess())
480 $this->isSaveExecuting =
false;
482 return $result->addErrors($r->getErrors());
486 $originalItemsValues = $this->getOriginalItemsValues();
489 foreach ($this->collection as $basketItem)
491 $r = $basketItem->save();
492 if (!$r->isSuccess())
494 $result->addErrors($r->getErrors());
497 if (isset($originalItemsValues[$basketItem->getId()]))
499 unset($originalItemsValues[$basketItem->getId()]);
503 if ($originalItemsValues)
505 foreach ($originalItemsValues as $itemValues)
507 $this->callEventOnBeforeSaleBasketItemDeleted($itemValues);
517 $r = $this->callEventOnSaleBasketSaved();
518 if (!$r->isSuccess())
520 $result->addErrors($r->getErrors());
524 $this->clearChanged();
526 $this->isSaveExecuting =
false;
534 private function checkCallingContext() : void
540 && !
$order->isSaveRunning()
543 trigger_error(
"Incorrect call to the save process. Use method save() on \Bitrix\Sale\Order entity.", E_USER_WARNING);
551 private function callEventOnBeforeSaleBasketItemDeleted($itemValues)
553 $itemValues[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
555 $event =
new Main\Event(
'sale',
"OnBeforeSaleBasketItemDeleted",
array(
'VALUES' => $itemValues));
565 $itemValues[
'ENTITY_REGISTRY_TYPE'] = static::getRegistryType();
574 protected function callEventOnSaleBasketBeforeSaved()
582 array(
'ENTITY' => $this)
589 foreach (
$event->getResults() as $eventResult)
591 if ($eventResult->getType() ==
Main\EventResult::ERROR)
595 'SALE_EVENT_ON_BEFORE_BASKET_SAVED'
597 if ($eventResultData = $eventResult->getParameters())
599 if (isset($eventResultData) && $eventResultData instanceof
ResultError)
617 protected function callEventOnSaleBasketSaved()
630 foreach(
$event->getResults() as $eventResult)
632 if($eventResult->getType() == Main\EventResult::ERROR)
635 Main\Localization\Loc::getMessage(
'SALE_EVENT_ON_BASKET_SAVED'),
636 'SALE_EVENT_ON_BASKET_SAVED'
638 if ($eventResultData = $eventResult->getParameters())
640 if (isset($eventResultData) && $eventResultData instanceof ResultError)
683 if ($this->fUserId ===
null)
729 if (!$r->isSuccess())
731 $result->addErrors($r->getErrors());
733 elseif ($r->hasWarnings())
735 $result->addWarnings($r->getWarnings());
746 public function refresh(RefreshStrategy $strategy =
null)
748 $isStartField = $this->isStartField();
754 $r =
$order->onBeforeBasketRefresh();
755 if (!$r->isSuccess())
761 if ($strategy ===
null)
763 $strategy = RefreshFactory::create();
766 $result = $strategy->refresh($this);
770 $r =
$order->onAfterBasketRefresh();
771 if (!$r->isSuccess())
777 $changedBasketItems =
$result->get(
'CHANGED_BASKET_ITEMS');
778 if (!empty($changedBasketItems))
784 $r =
$order->refreshData(
array(
'PRICE',
'PRICE_DELIVERY'));
785 if (!$r->isSuccess())
787 $result->addErrors($r->getErrors());
794 $hasMeaningfulFields = $this->hasMeaningfulField();
797 $r = $this->doFinalAction($hasMeaningfulFields);
798 if (!$r->isSuccess())
800 $result->addErrors($r->getErrors());
811 public function getOrderableItems()
814 $basket = static::create($this->
getSiteId());
816 if ($this->isLoadForFUserId)
823 $basket->setOrder(
$order);
826 $sortedCollection = $this->collection;
827 usort($sortedCollection,
function(BasketItemBase
$a, BasketItemBase $b){
828 return (
int)
$a->getField(
'SORT') - (int)$b->getField(
'SORT');
832 foreach ($sortedCollection as $item)
834 if (!$item->canBuy() || $item->isDelay())
837 $item->setCollection($basket);
838 $basket->addItem($item);
875 public function getContext()
901 public function getQuantityList()
903 $quantityList =
array();
909 foreach ($this->collection as $basketKey => $basketItem)
911 $quantityList[$basketItem->getBasketCode()] = $basketItem->getQuantity();
914 return $quantityList;
927 public function deleteItem($index)
929 $oldItem = parent::deleteItem($index);
931 unset($this->basketItemIndexMap[$oldItem->getBasketCode()]);
951 public function applyDiscount(
array $basketRows)
955 if ($this->
count() == 0 || empty($basketRows))
959 foreach ($this->collection as $basketItem)
961 if ($basketItem->isCustomPrice())
963 $basketCode = $basketItem->getBasketCode();
964 if (!isset($basketRows[$basketCode]))
967 $fields = $basketRows[$basketCode];
975 && $basketItem->getPrice() !=
$fields[
'PRICE'])
978 $basketItem->setFieldNoDemand(
'PRICE',
$fields[
'PRICE']);
981 if ($basketItem->getDiscountPrice() !=
$fields[
'DISCOUNT_PRICE'])
984 $basketItem->setFieldNoDemand(
'DISCOUNT_PRICE',
$fields[
'DISCOUNT_PRICE']);
987 if (isset(
$fields[
'DISCOUNT_VALUE']))
988 $basketItem->setFieldNoDemand(
'DISCOUNT_VALUE',
$fields[
'DISCOUNT_VALUE']);
991 unset(
$fields, $basketCode, $basketItem);
1002 public function createClone(\SplObjectStorage $cloneEntity =
null)
1004 if ($cloneEntity ===
null)
1006 $cloneEntity = new \SplObjectStorage();
1010 $basketClone = parent::createClone($cloneEntity);
1014 if ($cloneEntity->contains($this->order))
1020 return $basketClone;
1035 public function copy()
1037 if($this->order !==
null)
1039 throw new Main\SystemException(
'Could not clone basket which has order.');
1042 $basket = static::create($this->siteId);
1044 foreach($this as $originalItem)
1046 $item = $basket->createItem($originalItem->getField(
"MODULE"), $originalItem->getProductId());
1047 $item->initFields($originalItem->getFields()->getValues());
1066 $basket = static::createBasketObject();
1067 $basket->setOrder(
$order);
1068 $basket->setSiteId(
$order->getSiteId());
1070 return $basket->loadFromDb(
array(
"=ORDER_ID" =>
$order->getId()));
1082 $basketItem = parent::addItem($basketItem);
1097 $basketItem = parent::bindItem($basketItem);
1099 $this->basketItemIndexMap[$basketItem->getBasketCode()] = $basketItem->
getInternalIndex();
1115 if ($refreshItem !==
null)
1117 $strategy = RefreshFactory::createSingle($refreshItem->getBasketCode());
1121 $strategy = RefreshFactory::create(RefreshFactory::TYPE_FULL);
1124 return $this->refresh($strategy);
1139 $propertyCollection = $item->getPropertyCollection();
1140 if ($propertyCollection)
1142 $propertyList = $propertyCollection->getPropertyValues();
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)