Beispiel #1
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotSaveException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     /**
      * @var $entity \Magento\Catalog\Api\Data\ProductLinkInterface
      */
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $links = [];
     $extensions = $this->dataObjectProcessor->buildOutputDataArray($entity->getExtensionAttributes(), 'Magento\\Catalog\\Api\\Data\\ProductLinkExtensionInterface');
     $extensions = is_array($extensions) ? $extensions : [];
     $data = $entity->__toArray();
     foreach ($extensions as $attributeCode => $attribute) {
         $data[$attributeCode] = $attribute;
     }
     unset($data['extension_attributes']);
     $data['product_id'] = $linkedProduct->getId();
     $links[$linkedProduct->getId()] = $data;
     try {
         $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
         $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
         $productData = $prodyctHydrator->extract($product);
         $this->linkResource->saveProductLinks($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $links, $linkTypesToId[$entity->getLinkType()]);
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return $entity;
 }
 /**
  * {@inheritdoc}
  */
 public function setProductLinks($sku, $type, array $items)
 {
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     if (!isset($linkTypes[$type])) {
         throw new NoSuchEntityException(__('Provided link type "%1" does not exist', $type));
     }
     $product = $this->productRepository->get($sku);
     $assignedSkuList = [];
     /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $link */
     foreach ($items as $link) {
         $assignedSkuList[] = $link->getLinkedProductSku();
     }
     $linkedProductIds = $this->productResource->getProductsIdsBySkus($assignedSkuList);
     $links = [];
     /** @var \Magento\Catalog\Api\Data\ProductLinkInterface[] $items*/
     foreach ($items as $link) {
         $data = $link->__toArray();
         $linkedSku = $link->getLinkedProductSku();
         if (!isset($linkedProductIds[$linkedSku])) {
             throw new NoSuchEntityException(__('Product with SKU "%1" does not exist', $linkedSku));
         }
         $data['product_id'] = $linkedProductIds[$linkedSku];
         $links[$linkedProductIds[$linkedSku]] = $data;
     }
     $this->linkInitializer->initializeLinks($product, [$type => $links]);
     try {
         $product->save();
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return true;
 }
Beispiel #3
0
 /**
  * {@inheritdoc}
  */
 public function setProductLinks($sku, $type, array $items)
 {
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     if (!isset($linkTypes[$type])) {
         throw new NoSuchEntityException(__('Provided link type "%1" does not exist', $type));
     }
     $product = $this->productRepository->get($sku);
     // Replace only links of the specified type
     $existingLinks = $product->getProductLinks();
     $newLinks = [];
     if (!empty($existingLinks)) {
         foreach ($existingLinks as $link) {
             if ($link->getLinkType() != $type) {
                 $newLinks[] = $link;
             }
         }
         $newLinks = array_merge($newLinks, $items);
     } else {
         $newLinks = $items;
     }
     $product->setProductLinks($newLinks);
     try {
         $this->productRepository->save($product);
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return true;
 }
Beispiel #4
0
 /**
  * Get link type id by code
  *
  * @param string $code
  * @throws NoSuchEntityException
  * @return int
  */
 public function getTypeIdByCode($code)
 {
     $types = $this->linkTypeProvider->getLinkTypes();
     if (isset($types[$code])) {
         return $types[$code];
     }
     throw new NoSuchEntityException('Unknown link type code is provided');
 }
Beispiel #5
0
 /**
  * @dataProvider getItemAttributesDataProvider
  */
 public function testGetItemAttributes($type, $typeId)
 {
     $attributes = [['code' => 'test_code_1', 'type' => 'test_type_1']];
     $expectedResult = [['attribute_code' => $attributes[0]['code'], 'value' => $attributes[0]['type']]];
     $objectMock = $this->getMock('\\Magento\\Framework\\Object', ['create'], [], '', false);
     $objectMock->expects($this->once())->method('create')->willReturn(['attribute_code' => $attributes[0]['code'], 'value' => $attributes[0]['type']]);
     $linkMock = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Link', ['getAttributes'], [], '', false);
     $linkMock->expects($this->once())->method('getAttributes')->willReturn($attributes);
     $this->linkFactoryMock->expects($this->once())->method('create')->with($typeId)->willReturn($linkMock);
     $this->linkAttributeBuilderMock->expects($this->once())->method('populateWithArray')->willReturn($objectMock);
     $this->assertEquals($expectedResult, $this->model->getItemAttributes($type));
 }
 /**
  * @dataProvider getItemAttributesDataProvider
  */
 public function testGetItemAttributes($type, $typeId)
 {
     $attributes = [['code' => 'test_code_1', 'type' => 'test_type_1']];
     $linkAttributeMock = $this->getMock('\\Magento\\Catalog\\Api\\Data\\ProductLinkAttributeInterface');
     $linkAttributeMock->expects($this->once())->method('setCode')->with($attributes[0]['code'])->willReturnSelf();
     $linkAttributeMock->expects($this->once())->method('setType')->with($attributes[0]['type'])->willReturnSelf();
     $expectedResult = [$linkAttributeMock];
     $linkMock = $this->getMock('\\Magento\\Catalog\\Model\\Product\\Link', ['getAttributes'], [], '', false);
     $linkMock->expects($this->once())->method('getAttributes')->willReturn($attributes);
     $this->linkFactoryMock->expects($this->once())->method('create')->with($typeId)->willReturn($linkMock);
     $this->linkAttributeFactoryMock->expects($this->once())->method('create')->willReturn($linkAttributeMock);
     $this->assertEquals($expectedResult, $this->model->getItemAttributes($type));
 }
Beispiel #7
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotDeleteException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
     $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
     $productData = $prodyctHydrator->extract($product);
     $linkId = $this->linkResource->getProductLinkId($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $linkedProduct->getId(), $linkTypesToId[$entity->getLinkType()]);
     if (!$linkId) {
         throw new NoSuchEntityException(__('Product with SKU %1 is not linked to product with SKU %2', $entity->getLinkedProductSku(), $entity->getSku()));
     }
     try {
         $this->linkResource->deleteProductLink($linkId);
     } catch (\Exception $exception) {
         throw new CouldNotDeleteException(__('Invalid data provided for linked products'));
     }
 }
Beispiel #8
0
 /**
  * {@inheritdoc}
  */
 public function getProductLinkTypes()
 {
     $output = [];
     foreach ($this->linkTypeProvider->getLinkTypes() as $type => $typeCode) {
         $data = [LinkType::TYPE => $type, LinkType::CODE => $typeCode];
         $output[] = $this->builder->populateWithArray($data)->create();
     }
     return $output;
 }
 /**
  * Process product links, creating new links, updating and deleting existing links
  *
  * @param \Magento\Catalog\Api\Data\ProductInterface $product
  * @param \Magento\Catalog\Api\Data\ProductLinkInterface[] $newLinks
  * @return $this
  * @throws NoSuchEntityException
  */
 private function processLinks(\Magento\Catalog\Api\Data\ProductInterface $product, $newLinks)
 {
     if ($newLinks === null) {
         // If product links were not specified, don't do anything
         return $this;
     }
     // Clear all existing product links and then set the ones we want
     $linkTypes = $this->linkTypeProvider->getLinkTypes();
     foreach (array_keys($linkTypes) as $typeName) {
         $this->linkInitializer->initializeLinks($product, [$typeName => []]);
     }
     // Set each linktype info
     if (!empty($newLinks)) {
         $productLinks = [];
         foreach ($newLinks as $link) {
             $productLinks[$link->getLinkType()][] = $link;
         }
         foreach ($productLinks as $type => $linksByType) {
             $assignedSkuList = [];
             /** @var \Magento\Catalog\Api\Data\ProductLinkInterface $link */
             foreach ($linksByType as $link) {
                 $assignedSkuList[] = $link->getLinkedProductSku();
             }
             $linkedProductIds = $this->resourceModel->getProductsIdsBySkus($assignedSkuList);
             $linksToInitialize = [];
             foreach ($linksByType as $link) {
                 $linkDataArray = $this->extensibleDataObjectConverter->toNestedArray($link, [], 'Magento\\Catalog\\Api\\Data\\ProductLinkInterface');
                 $linkedSku = $link->getLinkedProductSku();
                 if (!isset($linkedProductIds[$linkedSku])) {
                     throw new NoSuchEntityException(__('Product with SKU "%1" does not exist', $linkedSku));
                 }
                 $linkDataArray['product_id'] = $linkedProductIds[$linkedSku];
                 $linksToInitialize[$linkedProductIds[$linkedSku]] = $linkDataArray;
             }
             $this->linkInitializer->initializeLinks($product, [$type => $linksToInitialize]);
         }
     }
     $product->setProductLinks($newLinks);
     return $this;
 }
Beispiel #10
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;
 }
Beispiel #11
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;
 }
Beispiel #12
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;
 }