/** * Get export data for collection * * @return array * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ protected function getExportData() { $exportData = []; try { $rawData = $this->collectRawData(); $multirawData = $this->collectMultirawData(); $productIds = array_keys($rawData); $stockItemRows = $this->prepareCatalogInventory($productIds); $this->rowCustomizer->prepareData($this->_getEntityCollection(), $productIds); $this->setHeaderColumns($multirawData['customOptionsData'], $stockItemRows); $this->_headerColumns = $this->rowCustomizer->addHeaderColumns($this->_headerColumns); foreach ($rawData as $productId => $productData) { foreach ($productData as $storeId => $dataRow) { if ($storeId == Store::DEFAULT_STORE_ID && isset($stockItemRows[$productId])) { $dataRow = array_merge($dataRow, $stockItemRows[$productId]); } $this->appendMultirowData($dataRow, $multirawData); if ($dataRow) { $exportData[] = $dataRow; } } } } catch (\Exception $e) { $this->_logger->critical($e); } return $exportData; }
/** * Get export data for collection * * @return array */ protected function _getExportData() { $exportData = array(); try { $collection = $this->_getEntityCollection(); $validAttrCodes = $this->_getExportAttrCodes(); $defaultStoreId = \Magento\Store\Model\Store::DEFAULT_STORE_ID; $dataRows = array(); $rowCategories = array(); $rowWebsites = array(); $rowTierPrices = array(); $rowGroupPrices = array(); $rowMultiselects = array(); $mediaGalery = array(); // prepare multi-store values and system columns values foreach ($this->_storeIdToCode as $storeId => &$storeCode) { // go through all stores $collection->setStoreId($storeId); if ($defaultStoreId == $storeId) { $collection->addCategoryIds()->addWebsiteNamesToResult(); // tier and group price data getting only once $rowTierPrices = $this->_prepareTierPrices($collection->getAllIds()); $rowGroupPrices = $this->_prepareGroupPrices($collection->getAllIds()); // getting media gallery data $mediaGalery = $this->_prepareMediaGallery($collection->getAllIds()); } foreach ($collection as $itemId => $item) { // go through all products $rowIsEmpty = true; // row is empty by default foreach ($validAttrCodes as &$attrCode) { // go through all valid attribute codes $attrValue = $item->getData($attrCode); if (!empty($this->_attributeValues[$attrCode])) { if ($this->_attributeTypes[$attrCode] == 'multiselect') { $attrValue = explode(',', $attrValue); $attrValue = array_intersect_key($this->_attributeValues[$attrCode], array_flip($attrValue)); $rowMultiselects[$storeId][$itemId][$attrCode] = $attrValue; } else { if (isset($this->_attributeValues[$attrCode][$attrValue])) { $attrValue = $this->_attributeValues[$attrCode][$attrValue]; } else { $attrValue = null; } } } // do not save value same as default or not existent if ($storeId != $defaultStoreId && isset($dataRows[$itemId][$defaultStoreId][$attrCode]) && $dataRows[$itemId][$defaultStoreId][$attrCode] == $attrValue) { $attrValue = null; } if (is_scalar($attrValue)) { $dataRows[$itemId][$storeId][$attrCode] = $attrValue; // mark row as not empty $rowIsEmpty = false; } if (!empty($rowMultiselects[$storeId][$itemId][$attrCode])) { $rowIsEmpty = false; } } if ($rowIsEmpty) { // remove empty rows unset($dataRows[$itemId][$storeId]); } else { $attrSetId = $item->getAttributeSetId(); $dataRows[$itemId][$storeId][self::COL_STORE] = $storeCode; $dataRows[$itemId][$storeId][self::COL_ATTR_SET] = $this->_attrSetIdToName[$attrSetId]; $dataRows[$itemId][$storeId][self::COL_TYPE] = $item->getTypeId(); if ($defaultStoreId == $storeId) { $rowWebsites[$itemId] = array_intersect(array_keys($this->_websiteIdToCode), $item->getWebsites()); $rowCategories[$itemId] = $item->getCategoryIds(); } } $item = null; } $collection->clear(); } // remove unused categories $allCategoriesIds = array_merge(array_keys($this->_categories), array_keys($this->_rootCategories)); foreach ($rowCategories as &$categories) { $categories = array_intersect($categories, $allCategoriesIds); } // prepare catalog inventory information $productIds = array_keys($dataRows); $stockItemRows = $this->_prepareCatalogInventory($productIds); // prepare links information $linksRows = $this->_prepareLinks($productIds); $linkIdColPrefix = array(); foreach ($this->_linkTypeProvider->getLinkTypes() as $linkTypeName => $linkTypeId) { $linkIdColPrefix[$linkTypeId] = '_' . $linkTypeName . '_'; } $this->rowCustomizer->prepareData($this->_entityCollection, $productIds); // prepare custom options information $customOptionsData = array(); $customOptionsDataPre = array(); foreach ($this->_storeIdToCode as $storeId => &$storeCode) { $options = $this->_optionColFactory->create()->reset()->addTitleToResult($storeId)->addPriceToResult($storeId)->addProductToFilter($productIds)->addValuesToResult($storeId); foreach ($options as $option) { $row = array(); $productId = $option['product_id']; $optionId = $option['option_id']; $customOptions = isset($customOptionsDataPre[$productId][$optionId]) ? $customOptionsDataPre[$productId][$optionId] : array(); if ($defaultStoreId == $storeId) { $row['_custom_option_type'] = $option['type']; $row['_custom_option_title'] = $option['title']; $row['_custom_option_is_required'] = $option['is_require']; $row['_custom_option_price'] = $option['price'] . ($option['price_type'] == 'percent' ? '%' : ''); $row['_custom_option_sku'] = $option['sku']; $row['_custom_option_max_characters'] = $option['max_characters']; $row['_custom_option_sort_order'] = $option['sort_order']; // remember default title for later comparisons $defaultTitles[$option['option_id']] = $option['title']; } elseif ($option['title'] != $customOptions[0]['_custom_option_title']) { $row['_custom_option_title'] = $option['title']; } $values = $option->getValues(); if ($values) { $firstValue = array_shift($values); $priceType = $firstValue['price_type'] == 'percent' ? '%' : ''; if ($defaultStoreId == $storeId) { $row['_custom_option_row_title'] = $firstValue['title']; $row['_custom_option_row_price'] = $firstValue['price'] . $priceType; $row['_custom_option_row_sku'] = $firstValue['sku']; $row['_custom_option_row_sort'] = $firstValue['sort_order']; $defaultValueTitles[$firstValue['option_type_id']] = $firstValue['title']; } elseif ($firstValue['title'] != $customOptions[0]['_custom_option_row_title']) { $row['_custom_option_row_title'] = $firstValue['title']; } } if ($row) { if ($defaultStoreId != $storeId) { $row['_custom_option_store'] = $this->_storeIdToCode[$storeId]; } $customOptionsDataPre[$productId][$optionId][] = $row; } foreach ($values as $value) { $row = array(); $valuePriceType = $value['price_type'] == 'percent' ? '%' : ''; if ($defaultStoreId == $storeId) { $row['_custom_option_row_title'] = $value['title']; $row['_custom_option_row_price'] = $value['price'] . $valuePriceType; $row['_custom_option_row_sku'] = $value['sku']; $row['_custom_option_row_sort'] = $value['sort_order']; } elseif ($value['title'] != $customOptions[0]['_custom_option_row_title']) { $row['_custom_option_row_title'] = $value['title']; } if ($row) { if ($defaultStoreId != $storeId) { $row['_custom_option_store'] = $this->_storeIdToCode[$storeId]; } $customOptionsDataPre[$option['product_id']][$option['option_id']][] = $row; } } $option = null; } $options = null; } foreach ($customOptionsDataPre as $productId => &$optionsData) { $customOptionsData[$productId] = array(); foreach ($optionsData as $optionId => &$optionRows) { $customOptionsData[$productId] = array_merge($customOptionsData[$productId], $optionRows); } unset($optionRows, $optionsData); } unset($customOptionsDataPre); $this->_setHeaderColumns($customOptionsData, $stockItemRows); $this->_headerColumns = $this->rowCustomizer->addHeaderColumns($this->_headerColumns); foreach ($dataRows as $productId => &$productData) { foreach ($productData as $storeId => &$dataRow) { if ($defaultStoreId != $storeId) { $dataRow[self::COL_SKU] = null; $dataRow[self::COL_ATTR_SET] = null; $dataRow[self::COL_TYPE] = null; $dataRow[self::COL_VISIBILITY] = $productData[$defaultStoreId][self::COL_VISIBILITY]; } else { $dataRow[self::COL_STORE] = null; if (isset($stockItemRows[$productId])) { $dataRow = array_merge($dataRow, $stockItemRows[$productId]); } } $this->_updateDataWithCategoryColumns($dataRow, $rowCategories, $productId); if ($rowWebsites[$productId]) { $dataRow['_product_websites'] = $this->_websiteIdToCode[array_shift($rowWebsites[$productId])]; } if (!empty($rowTierPrices[$productId])) { $dataRow = array_merge($dataRow, array_shift($rowTierPrices[$productId])); } if (!empty($rowGroupPrices[$productId])) { $dataRow = array_merge($dataRow, array_shift($rowGroupPrices[$productId])); } if (!empty($mediaGalery[$productId])) { $dataRow = array_merge($dataRow, array_shift($mediaGalery[$productId])); } foreach ($linkIdColPrefix as $linkId => &$colPrefix) { if (!empty($linksRows[$productId][$linkId])) { $linkData = array_shift($linksRows[$productId][$linkId]); $dataRow[$colPrefix . 'position'] = $linkData['position']; $dataRow[$colPrefix . 'sku'] = $linkData['sku']; if (null !== $linkData['default_qty']) { $dataRow[$colPrefix . 'default_qty'] = $linkData['default_qty']; } } } if (!empty($customOptionsData[$productId])) { $dataRow = array_merge($dataRow, array_shift($customOptionsData[$productId])); } $dataRow = $this->rowCustomizer->addData($dataRow, $productId); if (!empty($rowMultiselects[$storeId][$productId])) { foreach ($rowMultiselects[$storeId][$productId] as $attrKey => $attrVal) { if (!empty($rowMultiselects[$storeId][$productId][$attrKey])) { $dataRow[$attrKey] = array_shift($rowMultiselects[$storeId][$productId][$attrKey]); } } } $exportData[] = $dataRow; // calculate largest links block $largestLinks = 0; if (isset($linksRows[$productId])) { $linksRowsKeys = array_keys($linksRows[$productId]); foreach ($linksRowsKeys as $linksRowsKey) { $largestLinks = max($largestLinks, count($linksRows[$productId][$linksRowsKey])); } } $additionalRowsCount = max(count($rowCategories[$productId]), count($rowWebsites[$productId]), $largestLinks); if (!empty($rowTierPrices[$productId])) { $additionalRowsCount = max($additionalRowsCount, count($rowTierPrices[$productId])); } if (!empty($rowGroupPrices[$productId])) { $additionalRowsCount = max($additionalRowsCount, count($rowGroupPrices[$productId])); } if (!empty($mediaGalery[$productId])) { $additionalRowsCount = max($additionalRowsCount, count($mediaGalery[$productId])); } if (!empty($customOptionsData[$productId])) { $additionalRowsCount = max($additionalRowsCount, count($customOptionsData[$productId])); } $additionalRowsCount = $this->rowCustomizer->getAdditionalRowsCount($additionalRowsCount, $productId); if (!empty($rowMultiselects[$storeId][$productId])) { foreach ($rowMultiselects[$storeId][$productId] as $attributes) { $additionalRowsCount = max($additionalRowsCount, count($attributes)); } } if ($additionalRowsCount) { for ($i = 0; $i < $additionalRowsCount; $i++) { $dataRow = array(); if ($defaultStoreId != $storeId) { $dataRow[self::COL_STORE] = $this->_storeIdToCode[$storeId]; } $this->_updateDataWithCategoryColumns($dataRow, $rowCategories, $productId); if ($rowWebsites[$productId]) { $dataRow['_product_websites'] = $this->_websiteIdToCode[array_shift($rowWebsites[$productId])]; } if (!empty($rowTierPrices[$productId])) { $dataRow = array_merge($dataRow, array_shift($rowTierPrices[$productId])); } if (!empty($rowGroupPrices[$productId])) { $dataRow = array_merge($dataRow, array_shift($rowGroupPrices[$productId])); } if (!empty($mediaGalery[$productId])) { $dataRow = array_merge($dataRow, array_shift($mediaGalery[$productId])); } foreach ($linkIdColPrefix as $linkId => &$colPrefix) { if (!empty($linksRows[$productId][$linkId])) { $linkData = array_shift($linksRows[$productId][$linkId]); $dataRow[$colPrefix . 'position'] = $linkData['position']; $dataRow[$colPrefix . 'sku'] = $linkData['sku']; if (null !== $linkData['default_qty']) { $dataRow[$colPrefix . 'default_qty'] = $linkData['default_qty']; } } } if (!empty($customOptionsData[$productId])) { $dataRow = array_merge($dataRow, array_shift($customOptionsData[$productId])); } $dataRow = $this->rowCustomizer->addData($dataRow, $productId); if (!empty($rowMultiselects[$storeId][$productId])) { foreach ($rowMultiselects[$storeId][$productId] as $attrKey => $attrVal) { if (!empty($rowMultiselects[$storeId][$productId][$attrKey])) { $dataRow[$attrKey] = array_shift($rowMultiselects[$storeId][$productId][$attrKey]); } } } $exportData[] = $dataRow; } } } } } catch (\Exception $e) { $this->_logger->logException($e); } return $exportData; }