Updater.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Quote\Item;
  7. use Magento\Catalog\Model\ProductFactory;
  8. use Magento\Framework\Locale\FormatInterface;
  9. use Magento\Framework\DataObject\Factory as ObjectFactory;
  10. use Magento\Quote\Model\Quote;
  11. use Magento\Quote\Model\Quote\Item;
  12. use Zend\Code\Exception\InvalidArgumentException;
  13. /**
  14. * Class Updater
  15. */
  16. class Updater
  17. {
  18. /**
  19. * @var ProductFactory
  20. */
  21. protected $productFactory;
  22. /**
  23. * @var FormatInterface
  24. */
  25. protected $localeFormat;
  26. /**
  27. * @var ObjectFactory
  28. */
  29. protected $objectFactory;
  30. /**
  31. * Serializer interface instance.
  32. *
  33. * @var \Magento\Framework\Serialize\Serializer\Json
  34. */
  35. private $serializer;
  36. /**
  37. * @param ProductFactory $productFactory
  38. * @param FormatInterface $localeFormat
  39. * @param ObjectFactory $objectFactory
  40. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  41. */
  42. public function __construct(
  43. ProductFactory $productFactory,
  44. FormatInterface $localeFormat,
  45. ObjectFactory $objectFactory,
  46. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  47. ) {
  48. $this->productFactory = $productFactory;
  49. $this->localeFormat = $localeFormat;
  50. $this->objectFactory = $objectFactory;
  51. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  52. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  53. }
  54. /**
  55. * Update quote item qty.
  56. *
  57. * Custom price is updated in case 'custom_price' value exists
  58. *
  59. * @param Item $item
  60. * @param array $info
  61. * @throws InvalidArgumentException
  62. * @return Updater
  63. * @SuppressWarnings(PHPMD.NPathComplexity)
  64. */
  65. public function update(Item $item, array $info)
  66. {
  67. if (!isset($info['qty'])) {
  68. throw new InvalidArgumentException(__('The qty value is required to update quote item.'));
  69. }
  70. $itemQty = $info['qty'];
  71. if ($item->getProduct()->getStockItem()) {
  72. if (!$item->getProduct()->getStockItem()->getIsQtyDecimal()) {
  73. $itemQty = (int)$info['qty'];
  74. } else {
  75. $item->setIsQtyDecimal(1);
  76. }
  77. }
  78. $itemQty = $itemQty > 0 ? $itemQty : 1;
  79. if (isset($info['custom_price'])) {
  80. $this->setCustomPrice($info, $item);
  81. } elseif ($item->hasData('custom_price')) {
  82. $this->unsetCustomPrice($item);
  83. }
  84. if (empty($info['action']) || !empty($info['configured'])) {
  85. $noDiscount = !isset($info['use_discount']);
  86. $item->setQty($itemQty);
  87. $item->setNoDiscount($noDiscount);
  88. $item->getProduct()->setIsSuperMode(true);
  89. $item->getProduct()->unsSkipCheckRequiredOption();
  90. $item->checkData();
  91. }
  92. return $this;
  93. }
  94. /**
  95. * Prepares custom price and sets into a BuyRequest object as option of quote item
  96. *
  97. * @param array $info
  98. * @param Item $item
  99. * @return void
  100. */
  101. protected function setCustomPrice(array $info, Item $item)
  102. {
  103. $itemPrice = $this->parseCustomPrice($info['custom_price']);
  104. /** @var \Magento\Framework\DataObject $infoBuyRequest */
  105. $infoBuyRequest = $item->getBuyRequest();
  106. if ($infoBuyRequest) {
  107. $infoBuyRequest->setCustomPrice($itemPrice);
  108. $infoBuyRequest->setValue($this->serializer->serialize($infoBuyRequest->getData()));
  109. $infoBuyRequest->setCode('info_buyRequest');
  110. $infoBuyRequest->setProduct($item->getProduct());
  111. $item->addOption($infoBuyRequest);
  112. }
  113. $item->setCustomPrice($itemPrice);
  114. $item->setOriginalCustomPrice($itemPrice);
  115. }
  116. /**
  117. * Unset custom_price data for quote item
  118. *
  119. * @param Item $item
  120. * @return void
  121. */
  122. protected function unsetCustomPrice(Item $item)
  123. {
  124. /** @var \Magento\Framework\DataObject $infoBuyRequest */
  125. $infoBuyRequest = $item->getBuyRequest();
  126. if ($infoBuyRequest->hasData('custom_price')) {
  127. $infoBuyRequest->unsetData('custom_price');
  128. $infoBuyRequest->setValue($this->serializer->serialize($infoBuyRequest->getData()));
  129. $infoBuyRequest->setCode('info_buyRequest');
  130. $infoBuyRequest->setProduct($item->getProduct());
  131. $item->addOption($infoBuyRequest);
  132. }
  133. $item->setData('custom_price', null);
  134. $item->setData('original_custom_price', null);
  135. }
  136. /**
  137. * Return formatted price
  138. *
  139. * @param float|int $price
  140. * @return float|int
  141. */
  142. protected function parseCustomPrice($price)
  143. {
  144. $price = $this->localeFormat->getNumber($price);
  145. return $price > 0 ? $price : 0;
  146. }
  147. }