/**
  * Create product model from imported data for URL rewrite purposes.
  *
  * @param array $rowData
  *
  * @return ImportExport
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _populateForUrlGeneration($rowData)
 {
     $newSku = $this->import->getNewSku($rowData[ImportProduct::COL_SKU]);
     if (empty($newSku) || !isset($newSku['entity_id'])) {
         return null;
     }
     if ($this->import->getRowScope($rowData) == ImportProduct::SCOPE_STORE && empty($rowData[self::URL_KEY_ATTRIBUTE_CODE])) {
         return null;
     }
     $rowData['entity_id'] = $newSku['entity_id'];
     $product = $this->catalogProductFactory->create();
     $product->setId($rowData['entity_id']);
     foreach ($this->vitalForGenerationFields as $field) {
         if (isset($rowData[$field])) {
             $product->setData($field, $rowData[$field]);
         }
     }
     $this->categoryCache[$rowData['entity_id']] = $this->import->getProductCategories($rowData['sku']);
     $this->websiteCache[$rowData['entity_id']] = $this->import->getProductWebsites($rowData['sku']);
     foreach ($this->websiteCache[$rowData['entity_id']] as $websiteId) {
         if (!isset($this->websitesToStoreIds[$websiteId])) {
             $this->websitesToStoreIds[$websiteId] = $this->storeManager->getWebsite($websiteId)->getStoreIds();
         }
     }
     $this->setStoreToProduct($product, $rowData);
     if ($this->isGlobalScope($product->getStoreId())) {
         $this->populateGlobalProduct($product);
     } else {
         $this->addProductToImport($product, $product->getStoreId());
     }
     return $this;
 }
Example #2
0
 /**
  * @param ImportProduct $import
  * @return void
  */
 protected function clearProductUrls(ImportProduct $import)
 {
     $oldSku = $import->getOldSku();
     while ($bunch = $import->getNextBunch()) {
         $idToDelete = [];
         foreach ($bunch as $rowNum => $rowData) {
             if ($import->validateRow($rowData, $rowNum) && ImportProduct::SCOPE_DEFAULT == $import->getRowScope($rowData)) {
                 $idToDelete[] = $oldSku[$rowData[ImportProduct::COL_SKU]]['entity_id'];
             }
         }
         foreach ($idToDelete as $productId) {
             $this->urlPersist->deleteByData([UrlRewrite::ENTITY_ID => $productId, UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE]);
         }
     }
 }
 /**
  * Validate row attributes. Pass VALID row data ONLY as argument.
  *
  * @param array $rowData
  * @param int $rowNum
  * @param bool $isNewProduct Optional
  * @return bool
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function isRowValid(array $rowData, $rowNum, $isNewProduct = true)
 {
     $error = false;
     $rowScope = $this->_entityModel->getRowScope($rowData);
     if (\Magento\CatalogImportExport\Model\Import\Product::SCOPE_NULL != $rowScope) {
         foreach ($this->_getProductAttributes($rowData) as $attrCode => $attrParams) {
             // check value for non-empty in the case of required attribute?
             if (isset($rowData[$attrCode]) && strlen($rowData[$attrCode])) {
                 $error |= !$this->_entityModel->isAttributeValid($attrCode, $attrParams, $rowData, $rowNum);
             } elseif ($this->_isAttributeRequiredCheckNeeded($attrCode) && $attrParams['is_required']) {
                 // For the default scope - if this is a new product or
                 // for an old product, if the imported doc has the column present for the attrCode
                 if (\Magento\CatalogImportExport\Model\Import\Product::SCOPE_DEFAULT == $rowScope && ($isNewProduct || array_key_exists($attrCode, $rowData))) {
                     $this->_entityModel->addRowError(\Magento\CatalogImportExport\Model\Import\Product::ERROR_VALUE_IS_REQUIRED, $rowNum, $attrCode);
                     $error = true;
                 }
             }
         }
     }
     $error |= !$this->_isParticularAttributesValid($rowData, $rowNum);
     return !$error;
 }
Example #4
0
 /**
  * @dataProvider getRowScopeDataProvider
  */
 public function testGetRowScope($rowData, $expectedResult)
 {
     $result = $this->importProduct->getRowScope($rowData);
     $this->assertEquals($expectedResult, $result);
 }