Ejemplo 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;
 }
Ejemplo n.º 2
0
 /**
  * @param int[] $productIds
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function getCustomOptionsData($productIds)
 {
     $customOptionsData = [];
     foreach (array_keys($this->_storeIdToCode) as $storeId) {
         if (Store::DEFAULT_STORE_ID != $storeId) {
             continue;
         }
         $options = $this->_optionColFactory->create();
         /* @var \Magento\Catalog\Model\Resource\Product\Option\Collection $options*/
         $options->addOrder('sort_order');
         $options->reset()->addOrder('sort_order')->addTitleToResult($storeId)->addPriceToResult($storeId)->addProductToFilter($productIds)->addValuesToResult($storeId);
         foreach ($options as $option) {
             $row = [];
             $productId = $option['product_id'];
             $row['name'] = $option['title'];
             $row['type'] = $option['type'];
             $row['required'] = $option['is_require'];
             $row['price'] = $option['price'];
             $row['price_type'] = $option['price_type'] == 'percent' ? $option['price_type'] : 'fixed';
             $row['sku'] = $option['sku'];
             $values = $option->getValues();
             if ($values) {
                 foreach ($values as $value) {
                     $valuePriceType = $value['price_type'] == 'percent' ? $value['price_type'] : 'fixed';
                     $row['option_title'] = $value['title'];
                     $row['price'] = $value['price'];
                     $row['price_type'] = $valuePriceType;
                     $row['sku'] = $value['sku'];
                     $customOptionsData[$productId][$storeId][] = $this->optionRowToCellString($row);
                 }
             } else {
                 $customOptionsData[$productId][$storeId][] = $this->optionRowToCellString($row);
             }
             $option = null;
         }
         $options = null;
     }
     return $customOptionsData;
 }
Ejemplo n.º 3
0
 /**
  *  Returns actual product data: current id, options, options data and option values
  *
  * @param \Magento\Catalog\Model\Resource\Product\Option\Collection $options
  * @return array
  */
 protected function _getActualOptionsData(\Magento\Catalog\Model\Resource\Product\Option\Collection $options)
 {
     $actualOptionId = 0;
     $actualOptions = [];
     // array of type and title types, key is element ID
     $actualData = [];
     // array of option data
     $actualValues = [];
     // array of option values data
     /** @var $option \Magento\Catalog\Model\Product\Option */
     foreach ($options->getItems() as $option) {
         $lastOptionKey = $option->getType() . '|' . $option->getTitle();
         $actualOptionId++;
         if (!in_array($lastOptionKey, $actualOptions)) {
             $actualOptions[$actualOptionId] = $lastOptionKey;
             $actualData[$actualOptionId] = $this->_getOptionData($option);
             if ($optionValues = $this->_getOptionValues($option)) {
                 $actualValues[$actualOptionId] = $optionValues;
             }
         }
     }
     return ['id' => $actualOptionId, 'options' => $actualOptions, 'data' => $actualData, 'values' => $actualValues];
 }
Ejemplo n.º 4
0
 /**
  * Load exiting custom options data
  *
  * @return $this
  */
 protected function _initOldCustomOptions()
 {
     if (!$this->_oldCustomOptions) {
         $oldCustomOptions = array();
         $optionTitleTable = $this->_tables['catalog_product_option_title'];
         $productIds = array_values($this->_productsSkuToId);
         foreach ($this->_storeCodeToId as $storeId) {
             $addCustomOptions = function (\Magento\Catalog\Model\Product\Option $customOption) use(&$oldCustomOptions, $storeId) {
                 $productId = $customOption->getProductId();
                 if (!isset($oldCustomOptions[$productId])) {
                     $oldCustomOptions[$productId] = array();
                 }
                 if (isset($oldCustomOptions[$productId][$customOption->getId()])) {
                     $oldCustomOptions[$productId][$customOption->getId()]['titles'][$storeId] = $customOption->getTitle();
                 } else {
                     $oldCustomOptions[$productId][$customOption->getId()] = array('titles' => array($storeId => $customOption->getTitle()), 'type' => $customOption->getType());
                 }
             };
             /** @var $collection \Magento\Catalog\Model\Resource\Product\Option\Collection */
             $this->_optionCollection->reset();
             $this->_optionCollection->addProductToFilter($productIds);
             $this->_optionCollection->getSelect()->join(array('option_title' => $optionTitleTable), 'option_title.option_id = main_table.option_id', array('title' => 'title', 'store_id' => 'store_id'))->where('option_title.store_id = ?', $storeId);
             $this->_byPagesIterator->iterate($this->_optionCollection, $this->_pageSize, array($addCustomOptions));
         }
         $this->_oldCustomOptions = $oldCustomOptions;
     }
     return $this;
 }
Ejemplo n.º 5
0
 /**
  * @param int[] $productIds
  * @return array
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function getCustomOptionsData($productIds)
 {
     $customOptionsData = [];
     $customOptionsDataPre = [];
     foreach (array_keys($this->_storeIdToCode) as $storeId) {
         $options = $this->_optionColFactory->create()->reset()->addTitleToResult($storeId)->addPriceToResult($storeId)->addProductToFilter($productIds)->addValuesToResult($storeId);
         foreach ($options as $option) {
             $row = [];
             $productId = $option['product_id'];
             $optionId = $option['option_id'];
             if (Store::DEFAULT_STORE_ID == $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'];
             } else {
                 $row['_custom_option_title'] = $option['title'];
             }
             $values = $option->getValues();
             if ($values) {
                 $firstValue = array_shift($values);
                 $priceType = $firstValue['price_type'] == 'percent' ? '%' : '';
                 if (Store::DEFAULT_STORE_ID == $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'];
                 } else {
                     $row['_custom_option_row_title'] = $firstValue['title'];
                 }
             }
             if (Store::DEFAULT_STORE_ID != $storeId) {
                 $row['_custom_option_store'] = $this->_storeIdToCode[$storeId];
             }
             $customOptionsDataPre[$productId][$optionId][] = $row;
             if ($values) {
                 foreach ($values as $value) {
                     $row = [];
                     $valuePriceType = $value['price_type'] == 'percent' ? '%' : '';
                     if (Store::DEFAULT_STORE_ID == $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'];
                     } else {
                         $row['_custom_option_row_title'] = $value['title'];
                     }
                     if ($row) {
                         if (Store::DEFAULT_STORE_ID != $storeId) {
                             $row['_custom_option_store'] = $this->_storeIdToCode[$storeId];
                         }
                         $customOptionsDataPre[$option['product_id']][$optionId][] = $row;
                     }
                 }
             }
             $option = null;
         }
         $options = null;
     }
     foreach ($customOptionsDataPre as $productId => $optionsData) {
         $customOptionsData[$productId] = [];
         foreach ($optionsData as $optionId => $optionRows) {
             $customOptionsData[$productId] = array_merge($customOptionsData[$productId], $optionRows);
         }
     }
     return $customOptionsData;
 }
Ejemplo n.º 6
0
 public function testReset()
 {
     $this->collection->reset();
 }