Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
basketitemcollection.php
1<?php
2
3namespace Bitrix\Sale;
4
11
12Loc::loadMessages(__FILE__);
13
19{
29 public function createItem($moduleId, $productId, $basketCode = null)
30 {
31 $basketItem = static::createItemInternal($this, $moduleId, $productId, $basketCode);
32
33 $basketItem->setCollection($this);
34 $this->addItem($basketItem);
35
36 return $basketItem;
37 }
38
48 protected static function createItemInternal(BasketItemCollection $basket, $moduleId, $productId, $basketCode = null)
49 {
51 $basketItemClassName = static::getItemCollectionClassName();
52 return $basketItemClassName::create($basket, $moduleId, $productId, $basketCode);
53 }
54
58 public function getOrder()
59 {
60 $basket = $this->getBasket();
61 if ($basket)
62 return $basket->getOrder();
63
64 return null;
65 }
66
70 abstract public function getBasket();
71
78 public function loadFromArray(array $itemList)
79 {
81 $itemClassName = static::getItemCollectionClassName();
82
83 foreach ($itemList as $item)
84 {
85 $basketItem = $itemClassName::load($this, $item);
86 $this->addItem($basketItem);
87 }
88
89 $controller = Internals\CustomFieldsController::getInstance();
90 $controller->initializeCollection($this);
91 }
92
98 protected static function getItemCollectionClassName()
99 {
100 $registry = Registry::getInstance(static::getRegistryType());
101 return $registry->getBasketItemClassName();
102 }
103
109 public function getItemByBasketCode($code)
110 {
112 foreach ($this->collection as $basketItem)
113 {
114 $basketItem = $basketItem->findItemByBasketCode($code);
115 if ($basketItem != null)
116 {
117 return $basketItem;
118 }
119 }
120
121 return null;
122 }
123
130 public function getItemByXmlId($xmlId)
131 {
133 foreach ($this->collection as $basketItem)
134 {
135 $basketItem = $basketItem->findItemByXmlId($xmlId);
136 if ($basketItem != null)
137 {
138 return $basketItem;
139 }
140 }
141
142 return null;
143 }
144
150 public function getItemById($id)
151 {
152 $id = (int)$id;
153
154 if ($id <= 0)
155 {
156 return null;
157 }
158
160 foreach ($this->collection as $basketItem)
161 {
162 $item = $basketItem->findItemById($id);
163 if ($item !== null)
164 {
165 return $item;
166 }
167 }
168
169 return null;
170 }
171
175 public function getBasketItems()
176 {
177 return $this->collection;
178 }
179
190 public function getExistsItems(string $moduleId, int $productId, ?array $properties = [])
191 {
192 $result = [];
193
194 foreach ($this->getBasketItems() as $basketItem)
195 {
196 if ((int)$basketItem->getField('PRODUCT_ID') === $productId && $basketItem->getField('MODULE') === $moduleId)
197 {
198 // skip check properties
199 if (is_null($properties))
200 {
201 $result[] = $basketItem;
202 continue;
203 }
204
206 $basketPropertyCollection = $basketItem->getPropertyCollection();
207 if ($properties)
208 {
209 if ($basketPropertyCollection->isPropertyAlreadyExists($properties))
210 {
211 $result[] = $basketItem;
212 }
213 }
214 elseif (count($basketPropertyCollection) === 0)
215 {
216 $result[] = $basketItem;
217 }
218 }
219 }
220
221 return $result;
222 }
223
227 public function getOrderId()
228 {
229 $order = $this->getOrder();
230 if ($order)
231 return $order->getId();
232
233 return 0;
234 }
235
241 public function getContext()
242 {
243 global $USER;
244 $context = array();
245
247 $basketItem = $this->rewind();
248 if ($basketItem)
249 {
250 $siteId = $basketItem->getField('LID');
251 $fuserId = $basketItem->getFUserId();
252
253 $userId = Fuser::getUserIdById($fuserId);
254
255 if (empty($context['SITE_ID']))
256 {
257 $context['SITE_ID'] = $siteId;
258 }
259
260 if (empty($context['USER_ID']) && $userId > 0)
261 {
262 $context['USER_ID'] = $userId;
263 }
264 }
265
266 if (empty($context['SITE_ID']))
267 {
268 $context['SITE_ID'] = SITE_ID;
269 }
270
271 if (empty($context['USER_ID']))
272 {
273 $context['USER_ID'] = isset($USER) && $USER instanceof \CUser ? (int)$USER->GetID() : 0;
274 }
275
276 if (Loader::includeModule('currency'))
277 {
278 if (!empty($context['SITE_ID']))
279 {
280 $currency = Internals\SiteCurrencyTable::getSiteCurrency($context['SITE_ID']);
281 }
282
283 if (empty($currency))
284 {
285 $currency = Currency\CurrencyManager::getBaseCurrency();
286 }
287
288 if (!empty($currency) && Currency\CurrencyManager::checkCurrencyID($currency))
289 {
290 $context['CURRENCY'] = $currency;
291 }
292 }
293
294 return $context;
295 }
296
310 public function getExistsItem($moduleId, $productId, array $properties = array())
311 {
312 return current($this->getExistsItems($moduleId, $productId, $properties)) ?: null;
313 }
314}
static loadMessages($file)
Definition loc.php:64
createItem($moduleId, $productId, $basketCode=null)
getExistsItem($moduleId, $productId, array $properties=array())
static getUserIdById($fuserId)
Definition fuser.php:177
static getInstance($type)
Definition registry.php:183