2

My requirement is import custom csv file to custom table.

please share hints and key points to achieve this?

Thanks.

12

note: I get crystal clear explanation of custom import option from this link

Please first download the whole zip file from this github link then change the module name as per your requirement. Just alter the two files is enough to import your custom CSV to custom table.

step1: alter app/code/vendor/module_name/etc/import.xml from your download github folder.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/import.xsd">
    <entity name="chennai_event" label="Chennai Event" model="Gworks\Chennaievent\Model\Import\CustomerGroup" behaviorModel="Magento\ImportExport\Model\Source\Import\Behavior\Basic" />
</config>

step2: then you should need to create model class as per mention in the above import.xml

app/code/vendor/module_name/Model/Import/CustomerGroup.php

<?php
namespace Gworks\Chennaievent\Model\Import;

use Gworks\Chennaievent\Model\Import\CustomerGroup\RowValidatorInterface as ValidatorInterface;
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
use Magento\Framework\App\ResourceConnection;

class CustomerGroup extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
{

    const ID = 'id';
    const EVENTNAME = 'event_name';
    const IMGURL = 'event_imgurl';
    const BANNERIMGURL = 'event_bannerimgurl';
    const DESC = 'event_description';
    const LDESC = 'event_longdescription';
    const PLACE = 'event_place';
    const DATE = 'event_date';
    const GIFT = 'event_suggestgift';
    const TYPE = 'type';

    const TABLE_Entity = 'chennai_event';

    /**
     * Validation failure message template definitions
     *
     * @var array
     */
    protected $_messageTemplates = [
    ValidatorInterface::ERROR_TITLE_IS_EMPTY => 'TITLE is empty',
    ];

     protected $_permanentAttributes = [self::ID];
    /**
     * If we should check column names
     *
     * @var bool
     */
    protected $needColumnCheck = true;
    protected $groupFactory;
    /**
     * Valid column names
     *
     * @array
     */
    protected $validColumnNames = [
    self::ID,
    self::EVENTNAME,
    self::IMGURL,
    self::BANNERIMGURL,
    self::DESC,
    self::LDESC,
    self::PLACE,
    self::DATE,
    self::GIFT,
    self::TYPE,
    ];

    /**
     * Need to log in import history
     *
     * @var bool
     */
    protected $logInHistory = true;

    protected $_validators = [];


    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_connection;
    protected $_resource;

    /**
     * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
     */
    public function __construct(
    \Magento\Framework\Json\Helper\Data $jsonHelper,
    \Magento\ImportExport\Helper\Data $importExportData,
    \Magento\ImportExport\Model\ResourceModel\Import\Data $importData,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\ImportExport\Model\ResourceModel\Helper $resourceHelper,
    \Magento\Framework\Stdlib\StringUtils $string,
    ProcessingErrorAggregatorInterface $errorAggregator,
    \Magento\Customer\Model\GroupFactory $groupFactory
    ) {
    $this->jsonHelper = $jsonHelper;
    $this->_importExportData = $importExportData;
    $this->_resourceHelper = $resourceHelper;
    $this->_dataSourceModel = $importData;
    $this->_resource = $resource;
    $this->_connection = $resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);
    $this->errorAggregator = $errorAggregator;
    $this->groupFactory = $groupFactory;
    }
    public function getValidColumnNames()
    {
    return $this->validColumnNames;
    }

    /**
     * Entity type code getter.
     *
     * @return string
     */
    public function getEntityTypeCode()
    {
    return 'chennai_event';
    }

    /**
     * Row validation.
     *
     * @param array $rowData
     * @param int $rowNum
     * @return bool
     */
    public function validateRow(array $rowData, $rowNum)
    {

    $title = false;

    if (isset($this->_validatedRows[$rowNum])) {
        return !$this->getErrorAggregator()->isRowInvalid($rowNum);
    }

    $this->_validatedRows[$rowNum] = true;
    // BEHAVIOR_DELETE use specific validation logic
       // if (\Magento\ImportExport\Model\Import::BEHAVIOR_DELETE == $this->getBehavior()) {
        if (!isset($rowData[self::ID]) || empty($rowData[self::ID])) {
            $this->addRowError(ValidatorInterface::ERROR_TITLE_IS_EMPTY, $rowNum);
            return false;
        }

    return !$this->getErrorAggregator()->isRowInvalid($rowNum);
    }


    /**
     * Create Advanced price data from raw data.
     *
     * @throws \Exception
     * @return bool Result of operation.
     */
    protected function _importData()
    {
    if (\Magento\ImportExport\Model\Import::BEHAVIOR_DELETE == $this->getBehavior()) {
        $this->deleteEntity();
    } elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE == $this->getBehavior()) {
        $this->replaceEntity();
    } elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $this->getBehavior()) {
        $this->saveEntity();
    }

    return true;
    }
    /**
     * Save newsletter subscriber
     *
     * @return $this
     */
    public function saveEntity()
    {
    $this->saveAndReplaceEntity();
    return $this;
    }
    /**
     * Replace newsletter subscriber
     *
     * @return $this
     */
    public function replaceEntity()
    {
    $this->saveAndReplaceEntity();
    return $this;
    }
    /**
     * Deletes newsletter subscriber data from raw data.
     *
     * @return $this
     */
    public function deleteEntity()
    {
    $listTitle = [];
    while ($bunch = $this->_dataSourceModel->getNextBunch()) {
        foreach ($bunch as $rowNum => $rowData) {
            $this->validateRow($rowData, $rowNum);
            if (!$this->getErrorAggregator()->isRowInvalid($rowNum)) {
                $rowTtile = $rowData[self::ID];
                $listTitle[] = $rowTtile;
            }
            if ($this->getErrorAggregator()->hasToBeTerminated()) {
                $this->getErrorAggregator()->addRowToSkip($rowNum);
            }
        }
    }
    if ($listTitle) {
        $this->deleteEntityFinish(array_unique($listTitle),self::TABLE_Entity);
    }
    return $this;
    }
 /**
     * Save and replace newsletter subscriber
     *
     * @return $this
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    protected function saveAndReplaceEntity()
    {
    $behavior = $this->getBehavior();
    $listTitle = [];
    while ($bunch = $this->_dataSourceModel->getNextBunch()) {
        $entityList = [];
        foreach ($bunch as $rowNum => $rowData) {
            if (!$this->validateRow($rowData, $rowNum)) {
                $this->addRowError(ValidatorInterface::ERROR_TITLE_IS_EMPTY, $rowNum);
                continue;
            }
            if ($this->getErrorAggregator()->hasToBeTerminated()) {
                $this->getErrorAggregator()->addRowToSkip($rowNum);
                continue;
            }

            $rowTtile= $rowData[self::ID];
            $listTitle[] = $rowTtile;
            $entityList[$rowTtile][] = [
              self::ID => $rowData[self::ID],
              self::EVENTNAME => $rowData[self::EVENTNAME],
                self::IMGURL => $rowData[self::IMGURL],
                self::BANNERIMGURL => $rowData[self::BANNERIMGURL],
                self::DESC => $rowData[self::DESC],
                self::LDESC => $rowData[self::LDESC],
                self::PLACE => $rowData[self::PLACE],
                self::DATE => $rowData[self::DATE],
                self::GIFT => $rowData[self::GIFT],
                self::TYPE => $rowData[self::TYPE],
            ];
        }
        if (\Magento\ImportExport\Model\Import::BEHAVIOR_REPLACE == $behavior) {
            if ($listTitle) {
                if ($this->deleteEntityFinish(array_unique(  $listTitle), self::TABLE_Entity)) {
                    $this->saveEntityFinish($entityList, self::TABLE_Entity);
                }
            }
        } elseif (\Magento\ImportExport\Model\Import::BEHAVIOR_APPEND == $behavior) {
            $this->saveEntityFinish($entityList, self::TABLE_Entity);
        }
    }
    return $this;
    }
    /**
     * Save product prices.
     *
     * @param array $priceData
     * @param string $table
     * @return $this
     */
    protected function saveEntityFinish(array $entityData, $table)
    {
    if ($entityData) {
        $tableName = $this->_connection->getTableName($table);
        $entityIn = [];
        foreach ($entityData as $id => $entityRows) {
                foreach ($entityRows as $row) {
                    $entityIn[] = $row;
                }
        }
        if ($entityIn) {
            $this->_connection->insertOnDuplicate($tableName, $entityIn,[
                self::ID,
                self::EVENTNAME,
                self::IMGURL,
                self::BANNERIMGURL,
                self::DESC,
                self::LDESC,
                self::PLACE,
                self::DATE,
                self::GIFT,
                self::TYPE
        ]);
        }
    }
    return $this;
    }
    protected function deleteEntityFinish(array $listTitle, $table)
    {
    if ($table && $listTitle) {
            try {
                $this->countItemsDeleted += $this->_connection->delete(
                    $this->_connection->getTableName($table),
                    $this->_connection->quoteInto('customer_group_code IN (?)', $listTitle)
                );
                return true;
            } catch (\Exception $e) {
                return false;
            }

    } else {
        return false;
    }
    }
}

My table structure

easy for your reference

thats it, for any clarity related to this answer please mention in comment.

@Atish Goshwami answer also correct, Why am I update? because with my way I could achieve result within twenty minutes its simple way to done this.

  • Thanks Bilal, i got only little idea, can you please help me with this... I have a custom table..Now when i import csv by changing or deleting the product details, only the product ids of those updated products should be saved to table – Sushivam Jul 28 '16 at 6:45
  • I have implemented in Magento1x as shown here: magento.stackexchange.com/questions/127704/… – Sushivam Jul 28 '16 at 6:56
  • @SachinS did you resolved? it is working like native magento 2 import function. I don't know magento 1.X :-). if you mention what is your specific problem, I will try to help. – Bilal Usean Jul 30 '16 at 14:43
  • Bilal, I want to add only the modified product ids into my custom table during import...i have explained the problem here magento.stackexchange.com/questions/127903/…, need help – Sushivam Jul 30 '16 at 14:46
  • Any suggestions @Bilal Usean – Sushivam Jul 31 '16 at 6:43
7

I would suggest to extend the Magento 2's default import functionality.

To do that register a new module (lets say "Custom Import" for now)

Create a module.xml file at app/code/Foo/CustomImport/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Foo_CustomPriceImport" setup_version="1.0.0"/>
</config>

Next create import.xml file at app/code/Foo/CustomImport/etc/import.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/import.xsd">
    <entity name="custom_import" label="Custom Import" model="Foo\CustomImport\Model\Import\CustomImport" behaviorModel="Magento\ImportExport\Model\Source\Import\Behavior\Basic" />
</config>

The entry import.xml will add a new option in the import page "Entity Type" dropdown (System > Data Transfer > Import)

now in the import.xml the xml attributes:

behaviorModel: Takes the class name of the model class responsible to handle the logic for the import

label: Label name of the dropdown option

name: name for the dropdown option value

import page "Entity Type" dropdown

Next we need to create the model file at app/code/Foo/CustomImport/Model/Import/CustomPricing.php

class CustomImport extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity

You can take the reference of the Magento_AdvancedPricingImportExport to see the import logic.

  • Atish, i need your help in this link magento.stackexchange.com/questions/127704/… where i have a custom table, in which when i import csv products, those product ids should be saved in my custom table – Sushivam Jul 28 '16 at 6:24
  • @Atish, I followed the above-mentioned procedure, the check data functionality has worked, but after that, they show a button "Import" if I hit the button nothing is happening.could you pls share your thoughts – senthil Oct 17 '18 at 10:50
  • @senthil can you check the expection.log can see what is happening – Atish Goswami Oct 25 '18 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.