Skip to content

Magento 2 Custom Entity Export Issue – Data Not Exporting

I have created a custom entity type Product Inventory and added it to System → Data Transfer → Export. However, when I attempt to export the data, the CSV file only contains the header row, and no actual data is exported.

I need the following fields in the exported CSV:

  • Created At, Last Update Date, SKU, Entity ID, Qty, Price

Please assist in resolving this issue.

Here is my code

export.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/export.xsd">
<entity name="product_stock_export" label="Product Inventory" model="CustomReportsModelExportInventory" entityAttributeFilterType="product_stock_export"/>

CustomReportsModelExportInventory

 <?php

declare(strict_types=1);

namespace CustomReportsModelExport;

use Exception;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkDataCollection;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkModelAbstractModel;
use MagentoImportExportModelExport;
use MagentoImportExportModelExportFactory as ExportFactory;
use MagentoImportExportModelExportAbstractEntity;
use MagentoStoreModelStoreManagerInterface;
use MagentoImportExportModelResourceModelCollectionByPagesIteratorFactory;
use MagentoCatalogModelResourceModelProductCollectionFactory;


class Inventory extends AbstractEntity
{
    /**
     * @var AttributeCollectionProvider
     */
    private $attributeCollectionProvider;

    /**
     * @var CollectionFactory
     */
    protected $productCollection;

    /**
     * @var StoreRepositoryInterface
     */
    protected $storeRepository;

    /**
     * @var array
     */
    protected $storeIdAttributeCodes;

    private array $permanentAttributes = [
        'created_at' => ['label' => 'Created At'],
        'updated_at' => ['label' => 'Last Update Date'],
        // 'name' => ['label' => 'Item'],
        'sku' => ['label' => 'SKU'],
        'entity_id' => ['label' => 'Entity Id'],

        // 'qty' => ['label' => 'Stock Qty'],
        // 'price' => ['label' => 'Price'],
    ];

    
    public function __construct(
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager,
        ExportFactory $collectionFactory,
        CollectionByPagesIteratorFactory $resourceColFactory,
        AttributeCollectionProvider $attributeCollectionProvider,
        // StoreRepositoryInterface $storeRepository,
        CollectionFactory $productCollection,
        array $data = []
    ) {
        $this->attributeCollectionProvider = $attributeCollectionProvider;
        $this->productCollection                = $productCollection;

        parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data);
    }

    /**
     * @return Collection
     * @throws Exception
     */
    public function getAttributeCollection()
    {
        return $this->attributeCollectionProvider->get();
    }

    /**
     * @return string
     * @throws LocalizedException
     * @throws Exception
     */
    public function export()
    {
        $writer       = $this->getWriter();
        $writer->setHeaderCols($this->_getHeaderColumns());
        $collection = $this->productCollection->create();
        $collection->addAttributeToSelect(['created_at', 'updated_at', 'sku', 'entity_id']);
        $this->_exportCollectionByPages($collection);        

        return $writer->getContents();
    }



    /**
     * @return array
     * @throws Exception
     */
    protected function _getHeaderColumns()
    {
        return array_column($this->permanentAttributes, 'label');

    }  

    /**
     * @param AbstractModel $item
     */
    public function exportItem($item)
    {
        // not use this method
    }

    /**
     * @return string
     */
    public function getEntityTypeCode()
    {
        return 'product_stock_export';
    }

    /**
     * @return void
     */
    protected function _getEntityCollection()
    {
        // not use this method
    }
    
}

AttributeCollectionProvider.php

 <?php
namespace CustomReportsModelExport;

use Exception;
use MagentoEavModelEntityAttributeFactory;
use MagentoImportExportModelExportFactory as CollectionFactory;
use MagentoFrameworkDataCollection;

class AttributeCollectionProvider
{
    const OPTION_ID   = 'entity_id';

    /**
     * @var MagentoFrameworkDataCollection
     */
    private $collection;

    /**
     * @var AttributeFactory
     */
    private $attributeFactory;

    /**
     * @param CollectionFactory $collectionFactory
     * @param AttributeFactory $attributeFactory
     */
    public function __construct(
        CollectionFactory $collectionFactory,
        AttributeFactory $attributeFactory
    ) {
        $this->collection       = $collectionFactory->create(Collection::class);
        $this->attributeFactory = $attributeFactory;
    }

    /**
     * @return MagentoFrameworkDataCollection
     * @throws Exception
     */
    public function get()
    {
        $listAttributes = [
            [
                'code'  => 'sku',
                'label' => 'SKU',
                'type'  => 'varchar'
            ],
            [
                'code'  => 'created_at',
                'label' => 'Created At',
                'type'  => 'varchar'
            ],
            [
                'code'  => 'updated_at',
                'label' => 'Last Update Date',
                'type'  => 'varchar'
            ],
            [
                'code'  => self::OPTION_ID,
                'label' => 'Enity ID',
                'type'  => 'varchar'
            ],
        ];

        if (count($this->collection) === 0) {
            foreach ($listAttributes as $listAttribute) {
                $sourceCodeAttribute = $this->attributeFactory->create();
                $sourceCodeAttribute->setId($listAttribute['code']);
                $sourceCodeAttribute->setDefaultFrontendLabel($listAttribute['label']);
                $sourceCodeAttribute->setAttributeCode($listAttribute['code']);
                $sourceCodeAttribute->setBackendType($listAttribute['type']);
                if (isset($listAttribute['frontend_input']) && $listAttribute['frontend_input'] === 'select') {
                    $sourceCodeAttribute->setFrontendInput('select');                   
                }
                $this->collection->addItem($sourceCodeAttribute);
            }

        }

        return $this->collection;
    }
}