Bitrix-D7  20.5.0
basketitemcollection.php
См. документацию.
1 <?php
2 
3 namespace Bitrix\Sale;
4 
5 use Bitrix\Currency;
11 
12 Loc::loadMessages(__FILE__);
13 
14 /**
15  * Class BasketItemCollection
16  * @package Bitrix\Sale
17  */
19 {
20  /**
21  * @param $moduleId
22  * @param $productId
23  * @param null $basketCode
24  * @return BasketItemBase
25  * @throws NotImplementedException
26  * @throws \Bitrix\Main\ArgumentException
27  * @throws \Bitrix\Main\ArgumentTypeException
28  */
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 
39  /**
40  * @param BasketItemCollection $basket
41  * @param $moduleId
42  * @param $productId
43  * @param null $basketCode
44  * @return BasketItemBase
45  * @throws NotImplementedException
46  * @throws \Bitrix\Main\ArgumentException
47  */
48  protected static function createItemInternal(BasketItemCollection $basket, $moduleId, $productId, $basketCode = null)
49  {
50  /** @var BasketItem $basketItemClassName */
51  $basketItemClassName = static::getItemCollectionClassName();
52  return $basketItemClassName::create($basket, $moduleId, $productId, $basketCode);
53  }
54 
55  /**
56  * @return OrderBase
57  */
58  public function getOrder()
59  {
60  $basket = $this->getBasket();
61  if ($basket)
62  return $basket->getOrder();
63 
64  return null;
65  }
66 
67  /**
68  * @return BasketBase
69  */
70  abstract public function getBasket();
71 
72  /**
73  * @param array $itemList
74  * @throws NotImplementedException
75  * @throws \Bitrix\Main\ArgumentException
76  * @throws \Bitrix\Main\ArgumentTypeException
77  */
78  public function loadFromArray(array $itemList)
79  {
80  /** @var BasketItemBase $itemClassName */
81  $itemClassName = static::getItemCollectionClassName();
82 
83  foreach ($itemList as $item)
84  {
85  $basketItem = $itemClassName::load($this, $item);
86  $this->addItem($basketItem);
87  }
88 
90  $controller->initializeCollection($this);
91  }
92 
93  /**
94  * @return string
95  * @throws NotImplementedException
96  * @throws \Bitrix\Main\ArgumentException
97  */
98  protected static function getItemCollectionClassName()
99  {
100  $registry = Registry::getInstance(static::getRegistryType());
101  return $registry->getBasketItemClassName();
102  }
103 
104  /**
105  * @param $code
106  * @return BasketItemBase|null
107  * @throws \Bitrix\Main\ArgumentNullException
108  */
109  public function getItemByBasketCode($code)
110  {
111  /** @var BasketItemBase $basketItem */
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 
124  /**
125  * @param $id
126  * @return BasketItemBase|null
127  * @throws \Bitrix\Main\ArgumentNullException
128  */
129  public function getItemById($id)
130  {
131  if ($id <= 0)
132  return null;
133 
134  /** @var BasketItemBase $basketItem */
135  foreach ($this->collection as $basketItem)
136  {
137  $item = $basketItem->findItemById($id);
138  if ($item !== null)
139  return $item;
140  }
141 
142  return null;
143  }
144 
145  /**
146  * @return array
147  */
148  public function getBasketItems()
149  {
150  return $this->collection;
151  }
152 
153  /**
154  * @param $moduleId
155  * @param $productId
156  * @param array $properties
157  * @return BasketItem|null
158  * @throws NotImplementedException
159  * @throws \Bitrix\Main\ArgumentException
160  * @throws \Bitrix\Main\ArgumentNullException
161  */
162  public function getExistsItem($moduleId, $productId, array $properties = array())
163  {
164  /** @var BasketItem $basketItem */
165  foreach ($this->collection as $basketItem)
166  {
167  if ($basketItem->getField('PRODUCT_ID') == $productId && $basketItem->getField('MODULE') == $moduleId)
168  {
169  /** @var BasketPropertiesCollection $basketPropertyCollection */
170  $basketPropertyCollection = $basketItem->getPropertyCollection();
171  if (!empty($properties) && is_array($properties))
172  {
173  if ($basketPropertyCollection->isPropertyAlreadyExists($properties))
174  {
175  return $basketItem;
176  }
177  }
178  elseif (count($basketPropertyCollection) == 0)
179  {
180  return $basketItem;
181  }
182  }
183  }
184 
185  return null;
186  }
187 
188  /**
189  * @return int
190  */
191  public function getOrderId()
192  {
193  $order = $this->getOrder();
194  if ($order)
195  return $order->getId();
196 
197  return 0;
198  }
199 
200  /**
201  * @return array
202  * @throws \Bitrix\Main\ArgumentException
203  * @throws \Bitrix\Main\LoaderException
204  */
205  public function getContext()
206  {
207  global $USER;
208  $context = array();
209 
210  /** @var BasketItem $basketItem */
211  $basketItem = $this->rewind();
212  if ($basketItem)
213  {
214  $siteId = $basketItem->getField('LID');
215  $fuserId = $basketItem->getFUserId();
216 
217  $userId = Fuser::getUserIdById($fuserId);
218 
219  if (empty($context['SITE_ID']))
220  {
221  $context['SITE_ID'] = $siteId;
222  }
223 
224  if (empty($context['USER_ID']) && $userId > 0)
225  {
226  $context['USER_ID'] = $userId;
227  }
228  }
229 
230  if (empty($context['SITE_ID']))
231  {
232  $context['SITE_ID'] = SITE_ID;
233  }
234 
235  if (empty($context['USER_ID']))
236  {
237  $context['USER_ID'] = isset($USER) && $USER instanceof \CUser ? (int)$USER->GetID() : 0;
238  }
239 
240  if (Loader::includeModule('currency'))
241  {
242  if (!empty($context['SITE_ID']))
243  {
244  $currency = Internals\SiteCurrencyTable::getSiteCurrency($context['SITE_ID']);
245  }
246 
247  if (empty($currency))
248  {
250  }
251 
252  if (!empty($currency) && Currency\CurrencyManager::checkCurrencyID($currency))
253  {
254  $context['CURRENCY'] = $currency;
255  }
256  }
257 
258  return $context;
259  }
260 }
static getBaseCurrency()
Return base currency.
static checkCurrencyID($currency)
Check currency id.
static includeModule($moduleName)
Includes a module by its name.
static loadMessages($file)
Loads language messages for specified file in a lazy way.
Definition: loc.php:67
Exception is thrown when operation is not implemented but should be.
static createItemInternal(BasketItemCollection $basket, $moduleId, $productId, $basketCode=null)
createItem($moduleId, $productId, $basketCode=null)
getExistsItem($moduleId, $productId, array $properties=array())
static getUserIdById($fuserId)
Return user by fuserId.
Definition: fuser.php:124
count()
Count elements of an object.
rewind()
Rewind the Iterator to the first element.
static getSiteCurrency($siteId)
Returns site currency.