ItemConverter.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Model\Cart\Totals;
  7. use Magento\Catalog\Helper\Product\ConfigurationPool;
  8. use Magento\Framework\Api\DataObjectHelper;
  9. use Magento\Framework\Api\ExtensibleDataInterface;
  10. use Magento\Framework\Event\ManagerInterface as EventManager;
  11. /**
  12. * Cart item totals converter.
  13. *
  14. * @codeCoverageIgnore
  15. */
  16. class ItemConverter
  17. {
  18. /**
  19. * @var ConfigurationPool
  20. */
  21. private $configurationPool;
  22. /**
  23. * @var EventManager
  24. */
  25. private $eventManager;
  26. /**
  27. * @var \Magento\Quote\Api\Data\TotalsItemInterfaceFactory
  28. */
  29. private $totalsItemFactory;
  30. /**
  31. * @var \Magento\Framework\Api\DataObjectHelper
  32. */
  33. private $dataObjectHelper;
  34. /**
  35. * @var \Magento\Framework\Serialize\Serializer\Json
  36. */
  37. private $serializer;
  38. /**
  39. * Constructs a totals item converter object.
  40. *
  41. * @param ConfigurationPool $configurationPool
  42. * @param EventManager $eventManager
  43. * @param \Magento\Quote\Api\Data\TotalsItemInterfaceFactory $totalsItemFactory
  44. * @param DataObjectHelper $dataObjectHelper
  45. * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
  46. * @throws \RuntimeException
  47. */
  48. public function __construct(
  49. ConfigurationPool $configurationPool,
  50. EventManager $eventManager,
  51. \Magento\Quote\Api\Data\TotalsItemInterfaceFactory $totalsItemFactory,
  52. DataObjectHelper $dataObjectHelper,
  53. \Magento\Framework\Serialize\Serializer\Json $serializer = null
  54. ) {
  55. $this->configurationPool = $configurationPool;
  56. $this->eventManager = $eventManager;
  57. $this->totalsItemFactory = $totalsItemFactory;
  58. $this->dataObjectHelper = $dataObjectHelper;
  59. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
  60. ->get(\Magento\Framework\Serialize\Serializer\Json::class);
  61. }
  62. /**
  63. * Converts a specified rate model to a shipping method data object.
  64. *
  65. * @param \Magento\Quote\Model\Quote\Item $item
  66. * @return array
  67. * @throws \Exception
  68. */
  69. public function modelToDataObject($item)
  70. {
  71. $this->eventManager->dispatch('items_additional_data', ['item' => $item]);
  72. $items = $item->toArray();
  73. $items['options'] = $this->getFormattedOptionValue($item);
  74. unset($items[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
  75. $itemsData = $this->totalsItemFactory->create();
  76. $this->dataObjectHelper->populateWithArray(
  77. $itemsData,
  78. $items,
  79. \Magento\Quote\Api\Data\TotalsItemInterface::class
  80. );
  81. return $itemsData;
  82. }
  83. /**
  84. * Retrieve formatted item options view
  85. *
  86. * @param \Magento\Quote\Api\Data\CartItemInterface $item
  87. * @return string
  88. */
  89. private function getFormattedOptionValue($item)
  90. {
  91. $optionsData = [];
  92. /* @var $helper \Magento\Catalog\Helper\Product\Configuration */
  93. $helper = $this->configurationPool->getByProductType('default');
  94. $options = $this->configurationPool->getByProductType($item->getProductType())->getOptions($item);
  95. foreach ($options as $index => $optionValue) {
  96. $params = [
  97. 'max_length' => 55,
  98. 'cut_replacer' => ' <a href="#" class="dots tooltip toggle" onclick="return false">...</a>'
  99. ];
  100. $option = $helper->getFormattedOptionValue($optionValue, $params);
  101. $optionsData[$index] = $option;
  102. $optionsData[$index]['label'] = $optionValue['label'];
  103. }
  104. return $this->serializer->serialize($optionsData);
  105. }
  106. }