RowCustomizer.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\ConfigurableImportExport\Model\Export;
  8. use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
  9. use Magento\CatalogImportExport\Model\Export\RowCustomizerInterface;
  10. use Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
  11. use Magento\ConfigurableProduct\Model\Product\Type\Configurable as ConfigurableProductType;
  12. use Magento\ImportExport\Model\Import;
  13. use Magento\Store\Model\Store;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. /**
  16. * Customizes output during export
  17. */
  18. class RowCustomizer implements RowCustomizerInterface
  19. {
  20. /**
  21. * Header column for Configurable Product variations
  22. */
  23. const CONFIGURABLE_VARIATIONS_COLUMN = 'configurable_variations';
  24. /**
  25. * Header column for Configurable Product variation labels
  26. */
  27. const CONFIGURABLE_VARIATIONS_LABELS_COLUMN = 'configurable_variation_labels';
  28. /**
  29. * @var array
  30. */
  31. protected $configurableData = [];
  32. /**
  33. * @var string[]
  34. */
  35. private $configurableColumns = [
  36. self::CONFIGURABLE_VARIATIONS_COLUMN,
  37. self::CONFIGURABLE_VARIATIONS_LABELS_COLUMN
  38. ];
  39. /**
  40. * @var StoreManagerInterface
  41. */
  42. private $storeManager;
  43. /**
  44. * @param StoreManagerInterface $storeManager
  45. */
  46. public function __construct(StoreManagerInterface $storeManager)
  47. {
  48. $this->storeManager = $storeManager;
  49. }
  50. /**
  51. * Prepare configurable data for export
  52. *
  53. * @param ProductCollection $collection
  54. * @param int[] $productIds
  55. * @return void
  56. */
  57. public function prepareData($collection, $productIds)
  58. {
  59. $productCollection = clone $collection;
  60. $productCollection->addAttributeToFilter('entity_id', ['in' => $productIds])
  61. ->addAttributeToFilter('type_id', ['eq' => ConfigurableProductType::TYPE_CODE]);
  62. // set global scope during export
  63. $this->storeManager->setCurrentStore(Store::DEFAULT_STORE_ID);
  64. while ($product = $productCollection->fetchItem()) {
  65. $productAttributesOptions = $product->getTypeInstance()->getConfigurableOptions($product);
  66. $this->configurableData[$product->getId()] = [];
  67. $variations = [];
  68. $variationsLabels = [];
  69. foreach ($productAttributesOptions as $productAttributeOption) {
  70. foreach ($productAttributeOption as $optValues) {
  71. $variations[$optValues['sku']][] = $optValues['attribute_code'] . '=' . $optValues['option_title'];
  72. if (!empty($optValues['super_attribute_label'])) {
  73. $variationsLabels[$optValues['attribute_code']] = $optValues['attribute_code'] . '='
  74. . $optValues['super_attribute_label'];
  75. }
  76. }
  77. }
  78. foreach ($variations as $sku => $values) {
  79. $variations[$sku] = 'sku=' . $sku . Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR
  80. . implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $values);
  81. }
  82. $this->configurableData[$product->getId()] = [
  83. self::CONFIGURABLE_VARIATIONS_COLUMN => implode(
  84. ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR,
  85. $variations
  86. ),
  87. self::CONFIGURABLE_VARIATIONS_LABELS_COLUMN => implode(
  88. Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR,
  89. $variationsLabels
  90. )
  91. ];
  92. }
  93. }
  94. /**
  95. * Set headers columns
  96. *
  97. * @param array $columns
  98. * @return array
  99. */
  100. public function addHeaderColumns($columns)
  101. {
  102. return array_merge($columns, $this->configurableColumns);
  103. }
  104. /**
  105. * Add configurable data for export
  106. *
  107. * @param array $dataRow
  108. * @param int $productId
  109. * @return array
  110. */
  111. public function addData($dataRow, $productId)
  112. {
  113. if (!empty($this->configurableData[$productId])) {
  114. $dataRow = array_merge($dataRow, $this->configurableData[$productId]);
  115. }
  116. return $dataRow;
  117. }
  118. /**
  119. * Calculate the largest links block
  120. *
  121. * @param array $additionalRowsCount
  122. * @param int $productId
  123. * @return array
  124. */
  125. public function getAdditionalRowsCount($additionalRowsCount, $productId)
  126. {
  127. if (!empty($this->configurableData[$productId])) {
  128. $additionalRowsCount = max($additionalRowsCount, count($this->configurableData[$productId]));
  129. }
  130. return $additionalRowsCount;
  131. }
  132. }