Magento 2: How to create new import entity
If you want to add a custom import feature in your Magento 2 site, you can do this by following steps. This will add a new entity in ADMIN PANEL > SYSTEM > DATA TRANSFER > IMPORT
Vendor\Module\etc\import.xml
Vendor\Module\Model\Import.php
Vendor\Module\Files\Sample\mycustomimport.csv
Now, Open your console and run below command:
Navigate to ADMIN PANEL > SYSTEM > DATA TRANSFER > IMPORT > IMPORT SETTINGS > Click on the dropdown >. Your custom import entity will appear here.
Vendor\Module\etc\import.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?>
<!--
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/import.xsd">
<entity name="my_custom_import" label="My Custom Import"
model="Vendor\Module\Model\Import"
behaviorModel="Magento\ImportExport\Model\Source\Import\Behavior\Basic"
/>
</config>
Vendor\Module\Model\Import.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php
/**
* @author Reena Parekh <reena1.parekh@gmail.com>
*/
namespace Vendor\Module\Model;
use Magento\Framework\App\ResourceConnection;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
/**
* Class Import
*/
class Import extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{
const ENTITY_TYPE = 'my_custom_import';
const FIELD_ID_1 = 'column_name_1';
const FIELD_ID_2 = 'column_name_2';
const TABLE_ENTITY = 'my_module_table';
/**
* @var []
*/
protected $fields = [
self::FIELD_ID_1,
self::FIELD_ID_2
];
/**
* Need to log in import history
*
* @var bool
*/
protected $logInHistory = true;
/**
* Import constructor.
*
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\ImportExport\Helper\Data $importExportData
* @param \Magento\ImportExport\Model\ResourceModel\Import\Data $importData
* @param \Magento\Eav\Model\Config $config
* @param ResourceConnection $resource
* @param \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param ProcessingErrorAggregatorInterface $errorAggregator
*/
public function __construct(
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\ImportExport\Helper\Data $importExportData,
\Magento\ImportExport\Model\ResourceModel\Import\Data $importData,
\Magento\Eav\Model\Config $config, ResourceConnection $resource,
\Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
\Magento\Framework\Stdlib\StringUtils $string,
ProcessingErrorAggregatorInterface $errorAggregator
) {
$this->string = $string;
$this->jsonHelper = $jsonHelper;
$this->_resourceHelper = $resourceHelper;
$this->errorAggregator = $errorAggregator;
$this->_importExportData = $importExportData;
foreach ($this->errorMessageTemplates as $errorCode => $message) {
$this->getErrorAggregator()->addErrorMessageTemplate($errorCode, $message);
}
$this->_dataSourceModel = $importData;
$this->_connection = $resource->getConnection();
}
/**
* Entity type code getter.
*
* @return string
*/
public function getEntityTypeCode()
{
return self::ENTITY_TYPE;
}
/**
* Get error aggregator object
*
* @return ProcessingErrorAggregatorInterface
*/
public function getErrorAggregator()
{
return $this->errorAggregator;
}
/**
* Row validation.
*
* @param array $rowData
* @param int $rowNum
* @return bool
*/
public function validateRow(array $rowData, $rowNum): bool
{
if (isset($this->_validatedRows[$rowNum])) {
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}
$this->_validatedRows[$rowNum] = true;
foreach ($this->fields as $field) {
if (!isset($rowData[$field]) || empty($rowData[$field])) {
$this->addRowError(\Magento\ImportExport\Model\Import::FIELD_EMPTY_ATTRIBUTE_VALUE_CONSTANT, $rowNum);
return false;
}
}
return !$this->getErrorAggregator()->isRowInvalid($rowNum);
}
/**
* Create advanced question data from raw data.
*
* @throws \Magento\Framework\Exception\LocalizedException
* @return bool Result of operation.
*/
protected function _importData()
{
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
foreach ($bunch as $rowNum => $rowData) {
$sql = null;
if (!$this->validateRow($rowData, $rowNum)) {
$this->addRowError(ValidatorInterface::ERROR_MESSAGE_IS_EMPTY, $rowNum);
continue;
}
if ($this->getErrorAggregator()->hasToBeTerminated()) {
$this->getErrorAggregator()->addRowToSkip($rowNum);
continue;
}
/**
* Direct SQLs are not really the best way but when performing large imports this proves to be the fastest.
* Feel free to optimize this code as per your application/requirement.
*/
$sql = 'update '. self::TABLE_ENTITY. ' set '
. self::FIELD_ID_2 . '='. $rowData[self::FIELD_ID_2]
. ' where ' . self::FIELD_ID_1 . ' LIKE "' . $rowData[self::FIELD_ID_1] . '"; ';
$this->_connection->query($sql);
}
}
return true;
}
}
Vendor\Module\Files\Sample\mycustomimport.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
column_name_1
column_name_2
90
First Course
120
Second Course
Now, Open your console and run below command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rm -rf var/cache var/page_cache generated/ pub/static/ var/di var/view_preprocessed && php bin/magento setup:upgrade && php bin/magento setup:di:compile && php bin/magento setup:static-content:deploy -f
Navigate to ADMIN PANEL > SYSTEM > DATA TRANSFER > IMPORT > IMPORT SETTINGS > Click on the dropdown >. Your custom import entity will appear here.
Comments
Post a Comment