/** * Import data rows * * @return boolean * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _importData() { $this->_initProductsSku(); $nextOptionId = $this->_resourceHelper->getNextAutoincrement($this->_tables['catalog_product_option']); $nextValueId = $this->_resourceHelper->getNextAutoincrement($this->_tables['catalog_product_option_type_value']); $prevOptionId = 0; while ($bunch = $this->_dataSourceModel->getNextBunch()) { $products = []; $options = []; $titles = []; $prices = []; $typeValues = []; $typePrices = []; $typeTitles = []; $parentCount = []; $childCount = []; foreach ($bunch as $rowNumber => $rowData) { $multiRowData = $this->_getMultiRowFormat($rowData); foreach ($multiRowData as $optionData) { $combinedData = array_merge($rowData, $optionData); if (!$this->isRowAllowedToImport($combinedData, $rowNumber)) { continue; } if (!$this->_parseRequiredData($combinedData)) { continue; } $optionData = $this->_collectOptionMainData($combinedData, $prevOptionId, $nextOptionId, $products, $prices); if ($optionData != null) { $options[] = $optionData; } $this->_collectOptionTypeData($combinedData, $prevOptionId, $nextValueId, $typeValues, $typePrices, $typeTitles, $parentCount, $childCount); $this->_collectOptionTitle($combinedData, $prevOptionId, $titles); } } // Save prepared custom options data !!! if ($this->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) { $this->_deleteEntities(array_keys($products)); } if ($this->_isReadyForSaving($options, $titles, $typeValues)) { if ($this->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) { $this->_compareOptionsWithExisting($options, $titles, $prices, $typeValues); } $this->_saveOptions($options)->_saveTitles($titles)->_savePrices($prices)->_saveSpecificTypeValues($typeValues)->_saveSpecificTypePrices($typePrices)->_saveSpecificTypeTitles($typeTitles)->_updateProducts($products); } } return true; }
/** * Retrieve next customer entity id * * @return int */ protected function _getNextEntityId() { if (!$this->_nextEntityId) { $this->_nextEntityId = $this->_resourceHelper->getNextAutoincrement($this->_entityTable); } return $this->_nextEntityId++; }
/** * @param \Magento\Framework\Stdlib\String $string * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig * @param \Magento\ImportExport\Model\ImportFactory $importFactory * @param \Magento\ImportExport\Model\Resource\Helper $resourceHelper * @param \Magento\Framework\App\Resource $resource * @param array $data * @SuppressWarnings(PHPMD.NPathComplexity) */ public function __construct(\Magento\Framework\Stdlib\String $string, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\ImportExport\Model\ImportFactory $importFactory, \Magento\ImportExport\Model\Resource\Helper $resourceHelper, \Magento\Framework\App\Resource $resource, array $data = []) { $this->_scopeConfig = $scopeConfig; $this->_dataSourceModel = isset($data['data_source_model']) ? $data['data_source_model'] : $importFactory->create()->getDataSourceModel(); $this->_connection = isset($data['connection']) ? $data['connection'] : $resource->getConnection('write'); $this->string = $string; $this->_pageSize = isset($data['page_size']) ? $data['page_size'] : (static::XML_PATH_PAGE_SIZE ? (int) $this->_scopeConfig->getValue(static::XML_PATH_PAGE_SIZE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE) : 0); $this->_maxDataSize = isset($data['max_data_size']) ? $data['max_data_size'] : $resourceHelper->getMaxDataSize(); $this->_bunchSize = isset($data['bunch_size']) ? $data['bunch_size'] : (static::XML_PATH_BUNCH_SIZE ? (int) $this->_scopeConfig->getValue(static::XML_PATH_BUNCH_SIZE, \Magento\Store\Model\ScopeInterface::SCOPE_STORE) : 0); }
/** * Get next address entity ID * * @return int */ protected function _getNextEntityId() { if (!$this->_nextEntityId) { /** @var $addressResource \Magento\Customer\Model\Resource\Address */ $addressResource = $this->_addressFactory->create()->getResource(); $addressTable = $addressResource->getEntityTable(); $this->_nextEntityId = $this->_resourceHelper->getNextAutoincrement($addressTable); } return $this->_nextEntityId++; }
/** * Validate data rows and save bunches to DB. * * @return $this|void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _saveValidatedBunches() { $source = $this->_getSource(); $currentDataSize = 0; $bunchRows = []; $startNewBunch = false; $nextRowBackup = []; $maxDataSize = $this->_resourceHelper->getMaxDataSize(); $bunchSize = $this->_importExportData->getBunchSize(); $source->rewind(); $this->_dataSourceModel->cleanBunches(); while ($source->valid() || $bunchRows) { if ($startNewBunch || !$source->valid()) { $this->_dataSourceModel->saveBunch($this->getEntityTypeCode(), $this->getBehavior(), $bunchRows); $bunchRows = $nextRowBackup; $currentDataSize = strlen(serialize($bunchRows)); $startNewBunch = false; $nextRowBackup = []; } if ($source->valid()) { if ($this->_errorsCount >= $this->_errorsLimit) { // errors limit check return; } $rowData = $source->current(); $this->_processedRowsCount++; if ($this->validateRow($rowData, $source->key())) { // add row to bunch for save $rowData = $this->_prepareRowForDb($rowData); $rowSize = strlen($this->jsonHelper->jsonEncode($rowData)); $isBunchSizeExceeded = $bunchSize > 0 && count($bunchRows) >= $bunchSize; if ($currentDataSize + $rowSize >= $maxDataSize || $isBunchSizeExceeded) { $startNewBunch = true; $nextRowBackup = [$source->key() => $rowData]; } else { $bunchRows[$source->key()] = $rowData; $currentDataSize += $rowSize; } } $source->next(); } } return $this; }
/** * Get new supper attribute id. * * @return int */ protected function _getNextAttrId() { if (!$this->_nextAttrId) { $mainTable = $this->_resource->getTableName('catalog_product_super_attribute'); $this->_nextAttrId = $this->_resourceHelper->getNextAutoincrement($mainTable); } $this->_nextAttrId++; return $this->_nextAttrId; }
/** * Save product type specific data. * * @throws \Exception * @return \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function saveData() { $connection = $this->_entityModel->getConnection(); $mainTable = $this->_resource->getTableName('catalog_product_super_attribute'); $labelTable = $this->_resource->getTableName('catalog_product_super_attribute_label'); $priceTable = $this->_resource->getTableName('catalog_product_super_attribute_pricing'); $linkTable = $this->_resource->getTableName('catalog_product_super_link'); $relationTable = $this->_resource->getTableName('catalog_product_relation'); $newSku = $this->_entityModel->getNewSku(); $oldSku = $this->_entityModel->getOldSku(); $productSuperData = array(); $productData = null; $nextAttrId = $this->_resourceHelper->getNextAutoincrement($mainTable); if ($this->_entityModel->getBehavior() == \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND) { $this->_loadSkuSuperData(); } while ($bunch = $this->_entityModel->getNextBunch()) { $superAttributes = array('attributes' => array(), 'labels' => array(), 'pricing' => array(), 'super_link' => array(), 'relation' => array()); $this->_loadSkuSuperAttributeValues($bunch, $newSku, $oldSku); foreach ($bunch as $rowNum => $rowData) { if (!$this->_entityModel->isRowAllowedToImport($rowData, $rowNum)) { continue; } // remember SCOPE_DEFAULT row data $scope = $this->_entityModel->getRowScope($rowData); if (\Magento\CatalogImportExport\Model\Import\Product::SCOPE_DEFAULT == $scope) { $productData = $newSku[$rowData[\Magento\CatalogImportExport\Model\Import\Product::COL_SKU]]; if ($this->_type != $productData['type_id']) { $productData = null; continue; } $productId = $productData['entity_id']; $this->_processSuperData($productSuperData, $superAttributes); $productSuperData = array('product_id' => $productId, 'attr_set_code' => $productData['attr_set_code'], 'used_attributes' => empty($this->_skuSuperData[$productId]) ? array() : $this->_skuSuperData[$productId], 'assoc_ids' => array()); } elseif (null === $productData) { continue; } if (!empty($rowData['_super_products_sku'])) { if (isset($newSku[$rowData['_super_products_sku']])) { $productSuperData['assoc_ids'][$newSku[$rowData['_super_products_sku']]['entity_id']] = true; } elseif (isset($oldSku[$rowData['_super_products_sku']])) { $productSuperData['assoc_ids'][$oldSku[$rowData['_super_products_sku']]['entity_id']] = true; } } if (empty($rowData['_super_attribute_code'])) { continue; } $attrParams = $this->_superAttributes[$rowData['_super_attribute_code']]; if ($this->_getSuperAttributeId($productId, $attrParams['id'])) { $productSuperAttrId = $this->_getSuperAttributeId($productId, $attrParams['id']); } elseif (!isset($superAttributes['attributes'][$productId][$attrParams['id']])) { $productSuperAttrId = $nextAttrId++; $superAttributes['attributes'][$productId][$attrParams['id']] = array('product_super_attribute_id' => $productSuperAttrId, 'position' => 0); $superAttributes['labels'][] = array('product_super_attribute_id' => $productSuperAttrId, 'store_id' => 0, 'use_default' => 1, 'value' => $attrParams['frontend_label']); } if (isset($rowData['_super_attribute_option']) && strlen($rowData['_super_attribute_option'])) { $optionId = $attrParams['options'][strtolower($rowData['_super_attribute_option'])]; if (!isset($productSuperData['used_attributes'][$attrParams['id']][$optionId])) { $productSuperData['used_attributes'][$attrParams['id']][$optionId] = false; } if (!empty($rowData['_super_attribute_price_corr'])) { $superAttributes['pricing'][] = array('product_super_attribute_id' => $productSuperAttrId, 'value_index' => $optionId, 'is_percent' => '%' == substr($rowData['_super_attribute_price_corr'], -1), 'pricing_value' => (double) rtrim($rowData['_super_attribute_price_corr'], '%'), 'website_id' => 0); } } } // save last product super data $this->_processSuperData($productSuperData, $superAttributes); // remove old data if needed if ($this->_entityModel->getBehavior() != \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND && $superAttributes['attributes']) { $quoted = $connection->quoteInto('IN (?)', array_keys($superAttributes['attributes'])); $connection->delete($mainTable, "product_id {$quoted}"); $connection->delete($linkTable, "parent_id {$quoted}"); $connection->delete($relationTable, "parent_id {$quoted}"); } $mainData = array(); foreach ($superAttributes['attributes'] as $productId => $attributesData) { foreach ($attributesData as $attrId => $row) { $row['product_id'] = $productId; $row['attribute_id'] = $attrId; $mainData[] = $row; } } if ($mainData) { $connection->insertOnDuplicate($mainTable, $mainData); } if ($superAttributes['labels']) { $connection->insertOnDuplicate($labelTable, $superAttributes['labels']); } if ($superAttributes['pricing']) { $connection->insertOnDuplicate($priceTable, $superAttributes['pricing'], array('is_percent', 'pricing_value')); } if ($superAttributes['super_link']) { $connection->insertOnDuplicate($linkTable, $superAttributes['super_link']); } if ($superAttributes['relation']) { $connection->insertOnDuplicate($relationTable, $superAttributes['relation']); } } return $this; }