Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractItem.php
Go to the documentation of this file.
1 <?php
7 
10 
51  \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface
52 {
56  protected $_parentItem = null;
57 
61  protected $_children = [];
62 
66  protected $_messages = [];
67 
73  protected $_optionsByCode;
74 
78  protected $productRepository;
79 
83  protected $priceCurrency;
84 
96  public function __construct(
97  \Magento\Framework\Model\Context $context,
98  \Magento\Framework\Registry $registry,
99  \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
102  \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
103  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
104  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
105  array $data = []
106  ) {
107  parent::__construct(
108  $context,
109  $registry,
110  $extensionFactory,
112  $resource,
113  $resourceCollection,
114  $data
115  );
116  $this->productRepository = $productRepository;
117  $this->priceCurrency = $priceCurrency;
118  }
119 
125  abstract public function getQuote();
126 
132  abstract public function getAddress();
133 
139  public function getProduct()
140  {
141  $product = $this->_getData('product');
142  if ($product === null && $this->getProductId()) {
143  $product = clone $this->productRepository->getById(
144  $this->getProductId(),
145  false,
146  $this->getQuote()->getStoreId()
147  );
148  $this->setProduct($product);
149  }
150 
154  $product->setFinalPrice(null);
155  if (is_array($this->_optionsByCode)) {
156  $product->setCustomOptions($this->_optionsByCode);
157  }
158  return $product;
159  }
160 
168  public function getFileDownloadParams()
169  {
170  return null;
171  }
172 
178  public function beforeSave()
179  {
180  parent::beforeSave();
181  if ($this->getParentItem()) {
182  $this->setParentItemId($this->getParentItem()->getId());
183  }
184  return $this;
185  }
186 
193  public function setParentItem($parentItem)
194  {
195  if ($parentItem) {
196  $this->_parentItem = $parentItem;
197  $parentItem->addChild($this);
198  }
199  return $this;
200  }
201 
207  public function getParentItem()
208  {
209  return $this->_parentItem;
210  }
211 
217  public function getChildren()
218  {
219  return $this->_children;
220  }
221 
228  public function addChild($child)
229  {
230  $this->setHasChildren(true);
231  $this->_children[] = $child;
232  return $this;
233  }
234 
241  public function setMessage($messages)
242  {
243  $messagesExists = $this->getMessage(false);
244  if (!is_array($messages)) {
245  $messages = [$messages];
246  }
247  foreach ($messages as $message) {
248  if (!in_array($message, $messagesExists)) {
249  $this->addMessage($message);
250  }
251  }
252  return $this;
253  }
254 
261  public function addMessage($message)
262  {
263  $this->_messages[] = $message;
264  return $this;
265  }
266 
273  public function getMessage($string = true)
274  {
275  if ($string) {
276  return join("\n", $this->_messages);
277  }
278  return $this->_messages;
279  }
280 
287  public function removeMessageByText($text)
288  {
289  foreach ($this->_messages as $key => $message) {
290  if ($message == $text) {
291  unset($this->_messages[$key]);
292  }
293  }
294  return $this;
295  }
296 
302  public function clearMessage()
303  {
304  $this->unsMessage();
305  // For older compatibility, when we kept message inside data array
306  $this->_messages = [];
307  return $this;
308  }
309 
315  public function getStore()
316  {
317  return $this->getQuote()->getStore();
318  }
319 
325  public function checkData()
326  {
327  $this->setHasError(false);
328  $this->clearMessage();
329 
330  $qty = $this->_getData('qty');
331 
332  try {
333  $this->setQty($qty);
334  } catch (\Magento\Framework\Exception\LocalizedException $e) {
335  $this->setHasError(true);
336  $this->setMessage($e->getMessage());
337  } catch (\Exception $e) {
338  $this->setHasError(true);
339  $this->setMessage(__('Item qty declaration error'));
340  }
341 
342  try {
343  $this->getProduct()->getTypeInstance()->checkProductBuyState($this->getProduct());
344  } catch (\Magento\Framework\Exception\LocalizedException $e) {
345  $this->setHasError(true)->setMessage($e->getMessage());
346  $this->getQuote()->setHasError(
347  true
348  )->addMessage(
349  __('Some of the products below do not have all the required options.')
350  );
351  } catch (\Exception $e) {
352  $this->setHasError(true)->setMessage(__('Something went wrong during the item options declaration.'));
353  $this->getQuote()->setHasError(true)->addMessage(__('We found an item options declaration error.'));
354  }
355 
356  if ($this->getProduct()->getHasError()) {
357  $this->setHasError(true)->setMessage(__('Some of the selected options are not currently available.'));
358  $this->getQuote()->setHasError(true)->addMessage($this->getProduct()->getMessage(), 'options');
359  }
360 
361  if ($this->getHasConfigurationUnavailableError()) {
362  $this->setHasError(
363  true
364  )->setMessage(
365  __('Selected option(s) or their combination is not currently available.')
366  );
367  $this->getQuote()->setHasError(
368  true
369  )->addMessage(
370  __('Some item options or their combination are not currently available.'),
371  'unavailable-configuration'
372  );
373  $this->unsHasConfigurationUnavailableError();
374  }
375 
376  return $this;
377  }
378 
384  public function getQty()
385  {
386  return $this->_getData('qty');
387  }
388 
394  public function getTotalQty()
395  {
396  if ($this->getParentItem()) {
397  return $this->getQty() * $this->getParentItem()->getQty();
398  }
399  return $this->getQty();
400  }
401 
407  public function calcRowTotal()
408  {
409  $qty = $this->getTotalQty();
410  // Round unit price before multiplying to prevent losing 1 cent on subtotal
411  $total = $this->priceCurrency->round($this->getCalculationPriceOriginal()) * $qty;
412  $baseTotal = $this->priceCurrency->round($this->getBaseCalculationPriceOriginal()) * $qty;
413 
414  $this->setRowTotal($this->priceCurrency->round($total));
415  $this->setBaseRowTotal($this->priceCurrency->round($baseTotal));
416  return $this;
417  }
418 
425  public function getCalculationPrice()
426  {
427  $price = $this->_getData('calculation_price');
428  if ($price === null) {
429  if ($this->hasCustomPrice()) {
430  $price = $this->getCustomPrice();
431  } else {
432  $price = $this->getConvertedPrice();
433  }
434  $this->setData('calculation_price', $price);
435  }
436  return $price;
437  }
438 
445  public function getCalculationPriceOriginal()
446  {
447  $price = $this->_getData('calculation_price');
448  if ($price === null) {
449  if ($this->hasOriginalCustomPrice()) {
450  $price = $this->getOriginalCustomPrice();
451  } else {
452  $price = $this->getConvertedPrice();
453  }
454  $this->setData('calculation_price', $price);
455  }
456  return $price;
457  }
458 
464  public function getBaseCalculationPrice()
465  {
466  if (!$this->hasBaseCalculationPrice()) {
467  if ($this->hasCustomPrice()) {
468  $price = (double)$this->getCustomPrice();
469  if ($price) {
470  $rate = $this->priceCurrency->convert($price, $this->getStore()) / $price;
471  $price = $price / $rate;
472  }
473  } else {
474  $price = $this->getPrice();
475  }
476  $this->setBaseCalculationPrice($price);
477  }
478  return $this->_getData('base_calculation_price');
479  }
480 
487  {
488  if (!$this->hasBaseCalculationPrice()) {
489  if ($this->hasOriginalCustomPrice()) {
490  $price = (double)$this->getOriginalCustomPrice();
491  if ($price) {
492  $rate = $this->priceCurrency->convert($price, $this->getStore()) / $price;
493  $price = $price / $rate;
494  }
495  } else {
496  $price = $this->getPrice();
497  }
498  $this->setBaseCalculationPrice($price);
499  }
500  return $this->_getData('base_calculation_price');
501  }
502 
509  public function getOriginalPrice()
510  {
511  $price = $this->_getData('original_price');
512  if ($price === null) {
513  $price = $this->priceCurrency->convert($this->getBaseOriginalPrice(), $this->getStore());
514  $this->setData('original_price', $price);
515  }
516  return $price;
517  }
518 
525  public function setOriginalPrice($price)
526  {
527  return $this->setData('original_price', $price);
528  }
529 
535  public function getBaseOriginalPrice()
536  {
537  return $this->_getData('base_original_price');
538  }
539 
546  public function setCustomPrice($value)
547  {
548  $this->setCalculationPrice($value);
549  $this->setBaseCalculationPrice(null);
550  return $this->setData('custom_price', $value);
551  }
552 
558  public function getPrice()
559  {
560  return $this->_getData('price');
561  }
562 
569  public function setPrice($value)
570  {
571  $this->setBaseCalculationPrice(null);
572  $this->setConvertedPrice(null);
573  return $this->setData('price', $value);
574  }
575 
580  public function getConvertedPrice()
581  {
582  $price = $this->_getData('converted_price');
583  if ($price === null) {
584  $price = $this->priceCurrency->convert($this->getPrice(), $this->getStore());
585  $this->setData('converted_price', $price);
586  }
587  return $price;
588  }
589 
595  public function setConvertedPrice($value)
596  {
597  $this->setCalculationPrice(null);
598  $this->setData('converted_price', $value);
599  return $this;
600  }
601 
607  public function __clone()
608  {
609  $this->setId(null);
610  $this->_parentItem = null;
611  $this->_children = [];
612  $this->_messages = [];
613  return $this;
614  }
615 
622  public function isChildrenCalculated()
623  {
624  if ($this->getParentItem()) {
625  $calculate = $this->getParentItem()->getProduct()->getPriceType();
626  } else {
627  $calculate = $this->getProduct()->getPriceType();
628  }
629 
630  if (null !== $calculate &&
631  (int)$calculate === \Magento\Catalog\Model\Product\Type\AbstractType::CALCULATE_CHILD
632  ) {
633  return true;
634  }
635  return false;
636  }
637 
644  public function isShipSeparately()
645  {
646  if ($this->getParentItem()) {
647  $shipmentType = $this->getParentItem()->getProduct()->getShipmentType();
648  } else {
649  $shipmentType = $this->getProduct()->getShipmentType();
650  }
651 
652  if (null !== $shipmentType &&
653  (int)$shipmentType === \Magento\Catalog\Model\Product\Type\AbstractType::SHIPMENT_SEPARATELY
654  ) {
655  return true;
656  }
657  return false;
658  }
659 
666  public function getTotalDiscountAmount()
667  {
668  $totalDiscountAmount = 0;
669  /* \Magento\Quote\Model\Quote\Item\AbstractItem[] */
670  $children = $this->getChildren();
671  if (!empty($children) && $this->isChildrenCalculated()) {
672  foreach ($children as $child) {
673  $totalDiscountAmount += $child->getDiscountAmount();
674  }
675  } else {
676  $totalDiscountAmount = $this->getDiscountAmount();
677  }
678  return $totalDiscountAmount;
679  }
680 }
__()
Definition: __.php:13
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$resource
Definition: bulk.php:12
$price
$message
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory, AttributeValueFactory $customAttributeFactory, \Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
$value
Definition: gender.phtml:16
$children
Definition: actions.phtml:11