Magento – add the custom entity for export Magento 2

exportimportexportmagento2model

I want to export my custom entity from the admin like products, customers, etc… I have already created the code for the import for my custom entity and it is working fine. But now I want to add the same entity for the export my data. Please see attached image that how I want the functionality.

enter image description here

There is a simple database table with three fields. The table name is 'manufacturer_manufacturer' and fields name are following.
There is no any primary key.

  1. manufacturer
  2. model
  3. product_ids

So I want just export above data from my table only.

I want to add the option in dropdown like 'manufacturer'.If admin selects that value and export then all the data should be exported in the CSV format.

Best Answer

This is possible by following the below steps;

  1. Include export.xml in your module etc directory with a content similar to the below;

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/import.xsd">
        <entity
            name="manufacturer"
            label="Manufacturer Export"
            model="VendorName\Manufacturer\Model\Export\Manufacturer"
            entityAttributeFilterType="manufacturer" />
    </config>
    
  2. Ensure your export model class extend \Magento\ImportExport\Model\Export\AbstractEntity class, then override the abstract methods to add your concrete implementations. The export model class will be similar to the below;

namespace VendorName\Manufacturer\Model\Export;

/**
 * Class Manufacturer
 */
class Manufacturer extends \Magento\ImportExport\Model\Export\AbstractEntity
{
    /**
     * Permanent column names
     */
    const COLUMN_MANUFACTURER = 'manufacturer';
    const COLUMN_MODEL = 'model';
    const COLUMN_PROD_IDS = 'product_ids';

    /**
     * Permanent entity columns
     *
     * @var string[]
     */
    protected $_permanentAttributes = [
        self::COLUMN_MANUFACTURER,
        self::COLUMN_MODEL,
        self::COLUMN_PROD_IDS,
    ];

    public function export()
    {
        // TODO: Implement export() method.
    }

    public function exportItem($item)
    {
        // TODO: Implement exportItem() method.
    }

    public function getEntityTypeCode()
    {
        return  'manufacturer';
    }

    protected function _getHeaderColumns()
    {
        return $this->_permanentAttributes;
    }

    protected function _getEntityCollection()
    {
        // TODO: Implement _getEntityCollection() method.
    }
}

You can get more information on how to implement these abstract methods in \Magento\CustomerImportExport\Model\Export\Customer class.

I hope this helps.