diff --git a/Helper/Data.php b/Helper/Data.php index aff822d..091047a 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -5,6 +5,7 @@ use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Serialize\SerializerInterface; /** * Class Data @@ -26,18 +27,28 @@ class Data extends AbstractHelper */ protected $sourceFactory; + /** + * Json Serializer + * + * @var SerializerInterface + */ + public $serializer; + /** * Data Helper constructor * * @param Context $context * @param Factory $sourceFactory + * @param SerializerInterface $serializer */ public function __construct( Context $context, - Factory $sourceFactory + Factory $sourceFactory, + SerializerInterface $serializer ) { $this->coreConfig = $context->getScopeConfig(); $this->sourceFactory = $sourceFactory; + $this->serializer = $serializer; parent::__construct($context); } diff --git a/Model/Import/Product.php b/Model/Import/Product.php index ff83961..0e621e0 100755 --- a/Model/Import/Product.php +++ b/Model/Import/Product.php @@ -11,6 +11,7 @@ namespace Firebear\ImportExport\Model\Import; use Firebear\ImportExport\Api\UrlKeyManagerInterface; +use Magento\Catalog\Model\Product\Attribute\Backend\Sku; use Magento\CatalogImportExport\Model\Import\Product\TaxClassProcessor; use Magento\Framework\Stdlib\DateTime; use Magento\CatalogImportExport\Model\Import\Product as MagentoProduct; @@ -390,20 +391,6 @@ protected function saveProducts() $rowSku = $rowData[self::COL_SKU]; - $storeIds = $this->storeManager->getStore()->getId(); - $urlKey = isset($rowData[self::URL_KEY]) - ? $this->productUrl->formatUrlKey($rowData[self::URL_KEY]) - : $this->productUrl->formatUrlKey($rowData[self::COL_NAME]); - $isDuplicate = $this->isDuplicateUrlKey($urlKey, $rowSku, $storeIds); - if ($isDuplicate || $this->urlKeyManager->isUrlKeyExist($rowSku, $urlKey)) { - $urlKey = $this->productUrl->formatUrlKey( - $rowData[self::COL_NAME] . '-' . $rowData[self::COL_SKU] - ); - } - $rowData[self::URL_KEY] = $urlKey; - $this->urlKeyManager->addUrlKeys($rowSku, $urlKey); - $this->urlKeys = []; - if (!$rowSku) { $this->getErrorAggregator()->addRowToSkip($rowNum); continue; @@ -817,6 +804,11 @@ private function customFieldsMapping($rowData) $rowData = $this->_parseAdditionalAttributes($rowData); $rowData = $this->setStockUseConfigFieldsValues($rowData); + if (isset($this->_parameters['generate_url']) + && $this->_parameters['generate_url'] == 1 + ) { + $rowData = $this->generateUrlKey($rowData); + } if (array_key_exists('status', $rowData) && $rowData['status'] != \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED ) { @@ -1185,4 +1177,117 @@ private function getProductEntityLinkField() } return $this->productEntityLinkField; } + + /** + * Validate data rows and save bunches to DB. + * + * @return $this + */ + protected function _saveValidatedBunches() + { + $source = $this->_getSource(); + $currentDataSize = 0; + $bunchRows = []; + $startNewBunch = false; + $nextRowBackup = []; + $maxDataSize = $this->_resourceHelper->getMaxDataSize(); + $bunchSize = $this->_importExportData->getBunchSize(); + $skuSet = []; + + $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($this->getSerializer()->serialize($bunchRows)); + $startNewBunch = false; + $nextRowBackup = []; + } + if ($source->valid()) { + try { + $rowData = $source->current(); + if (array_key_exists('sku', $rowData)) { + $skuSet[$rowData['sku']] = true; + } + } catch (\InvalidArgumentException $e) { + $this->addRowError($e->getMessage(), $this->_processedRowsCount); + $this->_processedRowsCount++; + $source->next(); + continue; + } + + $rowData = $this->customFieldsMapping($rowData); + + $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(); + } + } + $this->checkUrlKeyDuplicates(); + $this->getOptionEntity()->validateAmbiguousData(); + $this->_processedEntitiesCount = (count($skuSet)) ? : $this->_processedRowsCount; + + return $this; + } + + /** + * @param $rowData + * @return mixed + * @throws \Magento\Framework\Exception\NoSuchEntityException + */ + private function generateUrlKey($rowData) + { + $sku = $this->getCorrectSkuAsPerLength($rowData); + $storeIds = $this->storeManager->getStore()->getId(); + $urlKey = isset($rowData[self::URL_KEY]) + ? $this->productUrl->formatUrlKey($rowData[self::URL_KEY]) + : $this->productUrl->formatUrlKey($rowData[self::COL_NAME]); + $isDuplicate = $this->isDuplicateUrlKey($urlKey, $sku, $storeIds); + if ($isDuplicate || $this->urlKeyManager->isUrlKeyExist($sku, $urlKey)) { + $urlKey = $this->productUrl->formatUrlKey( + $rowData[self::COL_NAME] . '-' . $rowData[self::COL_SKU] + ); + } + $rowData[self::URL_KEY] = $urlKey; + $this->urlKeyManager->addUrlKeys($sku, $urlKey); + + return $rowData; + } + + /** + * @param array $rowData + * + * @return mixed + */ + public function getCorrectSkuAsPerLength(array $rowData) + { + return strlen($rowData[self::COL_SKU]) > Sku::SKU_MAX_LENGTH ? + substr($rowData[self::COL_SKU], 0, Sku::SKU_MAX_LENGTH) : $rowData[self::COL_SKU]; + } + + /** + * @return \Magento\Framework\Serialize\Serializer\Json|\Magento\Framework\Serialize\SerializerInterface + */ + private function getSerializer() + { + return $this->_helper->serializer; + } } \ No newline at end of file diff --git a/Plugin/Block/Import/Form.php b/Plugin/Block/Import/Form.php index 61c5b88..5941ad0 100755 --- a/Plugin/Block/Import/Form.php +++ b/Plugin/Block/Import/Form.php @@ -101,6 +101,18 @@ public function beforeSetForm(\Magento\ImportExport\Block\Adminhtml\Import\Edit\ } } + $fileFieldset = $form->getElement('basic_behavior_fieldset'); + $fileFieldset->addField( + 'base_behavior generate_url', + 'checkbox', + [ + 'name' => 'generate_url', + 'label' => __('Generate Unique Url if Duplicate'), + 'title' => __('Generate Unique Url if Duplicate'), + 'value' => 1, + ] + ); + return [$form]; } } \ No newline at end of file diff --git a/view/adminhtml/templates/import/form/before.phtml b/view/adminhtml/templates/import/form/before.phtml index 0a71a30..963ea79 100755 --- a/view/adminhtml/templates/import/form/before.phtml +++ b/view/adminhtml/templates/import/form/before.phtml @@ -21,6 +21,7 @@ require(['jquery', 'Magento_Ui/js/modal/alert', 'prototype'], function(jQuery){ this.showBehavior(entity.val()); this.showImportSource(true); this.showSampleFile(entity.val()); + this.showGenerateUrlIfDuplicate(entity.val()); } else { this.showBehavior(false); this.showImportSource(false); @@ -66,7 +67,7 @@ require(['jquery', 'Magento_Ui/js/modal/alert', 'prototype'], function(jQuery){ action: newActionUrl, target: this.ifrElemName }]); - }, + }; /** * Show upload source dropdown @@ -83,6 +84,18 @@ require(['jquery', 'Magento_Ui/js/modal/alert', 'prototype'], function(jQuery){ } jQuery('.source-fieldset').find('._required').removeClass('required-entry').prop('disabled', true); }; + + /** + * Show Generate Unique Url if Duplicate + * @param entitySelected + */ + varienImport.showGenerateUrlIfDuplicate = function(entitySelected) { + if (entitySelected === 'catalog_product') { + jQuery('.generate_url').show(); + } else { + jQuery('.generate_url').hide(); + } + }; //]]> });