Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * @param array $dataRow
  * @param array $multiRawData
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 private function appendMultirowData(&$dataRow, &$multiRawData)
 {
     $productId = $dataRow['product_id'];
     $productLinkId = $dataRow['product_link_id'];
     $storeId = $dataRow['store_id'];
     $sku = $dataRow[self::COL_SKU];
     unset($dataRow['product_id']);
     unset($dataRow['product_link_id']);
     unset($dataRow['store_id']);
     unset($dataRow[self::COL_SKU]);
     if (Store::DEFAULT_STORE_ID == $storeId) {
         unset($dataRow[self::COL_STORE]);
         $this->updateDataWithCategoryColumns($dataRow, $multiRawData['rowCategories'], $productId);
         if (!empty($multiRawData['rowWebsites'][$productId])) {
             $websiteCodes = [];
             foreach ($multiRawData['rowWebsites'][$productId] as $productWebsite) {
                 $websiteCodes[] = $this->_websiteIdToCode[$productWebsite];
             }
             $dataRow[self::COL_PRODUCT_WEBSITES] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $websiteCodes);
             $multiRawData['rowWebsites'][$productId] = [];
         }
         if (!empty($multiRawData['mediaGalery'][$productLinkId])) {
             $additionalImages = [];
             $additionalImageLabels = [];
             $additionalImageIsDisabled = [];
             foreach ($multiRawData['mediaGalery'][$productLinkId] as $mediaItem) {
                 $additionalImages[] = $mediaItem['_media_image'];
                 $additionalImageLabels[] = $mediaItem['_media_label'];
                 if ($mediaItem['_media_is_disabled'] == true) {
                     $additionalImageIsDisabled[] = $mediaItem['_media_image'];
                 }
             }
             $dataRow['additional_images'] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $additionalImages);
             $dataRow['additional_image_labels'] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $additionalImageLabels);
             $dataRow['hide_from_product_page'] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $additionalImageIsDisabled);
             $multiRawData['mediaGalery'][$productLinkId] = [];
         }
         foreach ($this->_linkTypeProvider->getLinkTypes() as $linkTypeName => $linkId) {
             if (!empty($multiRawData['linksRows'][$productLinkId][$linkId])) {
                 $colPrefix = $linkTypeName . '_';
                 $associations = [];
                 foreach ($multiRawData['linksRows'][$productLinkId][$linkId] as $linkData) {
                     if ($linkData['default_qty'] !== null) {
                         $skuItem = $linkData['sku'] . ImportProduct::PAIR_NAME_VALUE_SEPARATOR . $linkData['default_qty'];
                     } else {
                         $skuItem = $linkData['sku'];
                     }
                     $associations[$skuItem] = $linkData['position'];
                 }
                 $multiRawData['linksRows'][$productLinkId][$linkId] = [];
                 asort($associations);
                 $dataRow[$colPrefix . 'skus'] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, array_keys($associations));
                 $dataRow[$colPrefix . 'position'] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, array_values($associations));
             }
         }
         $dataRow = $this->rowCustomizer->addData($dataRow, $productId);
     }
     if (!empty($this->collectedMultiselectsData[$storeId][$productId])) {
         foreach (array_keys($this->collectedMultiselectsData[$storeId][$productId]) as $attrKey) {
             if (!empty($this->collectedMultiselectsData[$storeId][$productId][$attrKey])) {
                 $dataRow[$attrKey] = implode(Import::DEFAULT_GLOBAL_MULTI_VALUE_SEPARATOR, $this->collectedMultiselectsData[$storeId][$productId][$attrKey]);
             }
         }
     }
     if (!empty($multiRawData['customOptionsData'][$productLinkId][$storeId])) {
         $customOptionsRows = $multiRawData['customOptionsData'][$productLinkId][$storeId];
         $multiRawData['customOptionsData'][$productLinkId][$storeId] = [];
         $customOptions = implode(ImportProduct::PSEUDO_MULTI_LINE_SEPARATOR, $customOptionsRows);
         $dataRow = array_merge($dataRow, ['custom_options' => $customOptions]);
     }
     if (empty($dataRow)) {
         return null;
     } elseif ($storeId != Store::DEFAULT_STORE_ID) {
         $dataRow[self::COL_STORE] = $this->_storeIdToCode[$storeId];
         if (isset($productData[Store::DEFAULT_STORE_ID][self::COL_VISIBILITY])) {
             $dataRow[self::COL_VISIBILITY] = $productData[Store::DEFAULT_STORE_ID][self::COL_VISIBILITY];
         }
     }
     $dataRow[self::COL_SKU] = $sku;
     return $dataRow;
 }
Exemplo n.º 3
0
 /**
  * @param array $dataRow
  * @param array $multirawData
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function addMultirowData($dataRow, $multirawData)
 {
     $result = [];
     $productId = $dataRow['product_id'];
     $storeId = $dataRow['store_id'];
     unset($dataRow['product_id']);
     unset($dataRow['store_id']);
     while (true) {
         if (Store::DEFAULT_STORE_ID == $storeId) {
             unset($dataRow[self::COL_STORE]);
             $this->updateDataWithCategoryColumns($dataRow, $multirawData['rowCategories'], $productId);
             if (!empty($multirawData['rowWebsites'][$productId])) {
                 $dataRow['_product_websites'] = $this->_websiteIdToCode[array_shift($multirawData['rowWebsites'][$productId])];
             }
             if (!empty($multirawData['rowTierPrices'][$productId])) {
                 $dataRow = array_merge($dataRow, array_shift($multirawData['rowTierPrices'][$productId]));
             }
             if (!empty($multirawData['rowGroupPrices'][$productId])) {
                 $dataRow = array_merge($dataRow, array_shift($multirawData['rowGroupPrices'][$productId]));
             }
             if (!empty($multirawData['mediaGalery'][$productId])) {
                 $dataRow = array_merge($dataRow, array_shift($multirawData['mediaGalery'][$productId]));
             }
             foreach ($this->_linkTypeProvider->getLinkTypes() as $linkTypeName => $linkId) {
                 if (!empty($multirawData['linksRows'][$productId][$linkId])) {
                     $colPrefix = '_' . $linkTypeName . '_';
                     $linkData = array_shift($multirawData['linksRows'][$productId][$linkId]);
                     $dataRow[$colPrefix . 'position'] = $linkData['position'];
                     $dataRow[$colPrefix . 'sku'] = $linkData['sku'];
                     if ($linkData['default_qty'] !== null) {
                         $dataRow[$colPrefix . 'default_qty'] = $linkData['default_qty'];
                     }
                 }
             }
             $dataRow = $this->rowCustomizer->addData($dataRow, $productId);
             if (!empty($multirawData['customOptionsData'][$productId])) {
                 $dataRow = array_merge($dataRow, array_shift($multirawData['customOptionsData'][$productId]));
             }
         }
         if (!empty($this->collectedMultiselectsData[$storeId][$productId])) {
             foreach (array_keys($this->collectedMultiselectsData[$storeId][$productId]) as $attrKey) {
                 if (!empty($this->collectedMultiselectsData[$storeId][$productId][$attrKey])) {
                     $dataRow[$attrKey] = array_shift($this->collectedMultiselectsData[$storeId][$productId][$attrKey]);
                 }
             }
         }
         if (empty($dataRow)) {
             break;
         } elseif ($storeId != Store::DEFAULT_STORE_ID) {
             $dataRow[self::COL_STORE] = $this->_storeIdToCode[$storeId];
             $dataRow[self::COL_SKU] = null;
             $dataRow[self::COL_ATTR_SET] = null;
             $dataRow[self::COL_TYPE] = null;
             if (isset($productData[Store::DEFAULT_STORE_ID][self::COL_VISIBILITY])) {
                 $dataRow[self::COL_VISIBILITY] = $productData[Store::DEFAULT_STORE_ID][self::COL_VISIBILITY];
             }
         }
         $result[] = $dataRow;
         $dataRow = [];
     }
     return $result;
 }