1

I want to create custom entity export which export data from inventory_source with custom product attribute like color, size

I have referred this and this link and created my export model file and its working fine but when i join collection with catelog_product or its eav table its give error

Below is my code

  1. Vendor/Module/etc/export.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_ImportExport:etc/export.xsd">
        <entity name="custmom_stock_source" label="custom stock source" model="Vendor\Module\Model\Export\Sources" entityAttributeFilterType="custmom_stock_source"/>
    </config>
    
  2. Vendor/Module/Model/Export/Source.php

        <?php
        namespace Vendor\Module\Model\Export;
    
    
        use Exception;
        use Magento\Framework\App\Config\ScopeConfigInterface;
            use Magento\ImportExport\Model\Export\Factory as ExportFactory;
            use Magento\ImportExport\Model\Export\AbstractEntity;
            use Magento\Inventory\Model\ResourceModel\SourceItem;
            use Magento\Inventory\Model\ResourceModel\SourceItem\Collection as SourceItemCollection;
            use Magento\InventoryImportExport\Model\Export\SourceItemCollectionFactoryInterface;
            use Magento\InventoryImportExport\Model\Export\ColumnProviderInterface;
            use Magento\Store\Model\StoreManagerInterface;
            use Magento\ImportExport\Model\ResourceModel\CollectionByPagesIteratorFactory;
    
            use Vendor\Module\Model\Export\AttributeCollectionProvider;
            use \Magento\Store\Model\Store;
    
           class Sources extends \Magento\ImportExport\Model\Export\AbstractEntity
           {
    
    
        /**
         * @var AttributeCollectionProvider
         */
        private $attributeCollectionProvider;
    
        /**
         * @var SourceItemCollectionFactoryInterface
         */
        private $sourceItemCollectionFactory;
    
        /**
         * @var ColumnProviderInterface
         */
        private $columnProvider;
    
        /**
         * Product collection
         *
         * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
         */
        protected $_entityCollectionFactory;
    
         /**
         * Product collection
         *
         * @var \Magento\Catalog\Model\ResourceModel\Product\Collection
         */
        protected $_entityCollection;
    
        /**
         * @param ScopeConfigInterface $scopeConfig
         * @param StoreManagerInterface $storeManager
         * @param ExportFactory $collectionFactory
         * @param CollectionByPagesIteratorFactory $resourceColFactory
         * @param AttributeCollectionProvider $attributeCollectionProvider
         * @param SourceItemCollectionFactoryInterface $sourceItemCollectionFactory
         * @param ColumnProviderInterface $columnProvider
         * @param array $data
         */
        public function __construct(
        ScopeConfigInterface $scopeConfig,
        StoreManagerInterface $storeManager,
        ExportFactory $collectionFactory,
        CollectionByPagesIteratorFactory $resourceColFactory,
        AttributeCollectionProvider $attributeCollectionProvider,
        SourceItemCollectionFactoryInterface $sourceItemCollectionFactory,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productcollectionFactory,
        ColumnProviderInterface $columnProvider,
        array $data = []
        ) {
        $this->_entityCollectionFactory = $productcollectionFactory;
        $this->attributeCollectionProvider = $attributeCollectionProvider;
        $this->sourceItemCollectionFactory = $sourceItemCollectionFactory;
        $this->columnProvider = $columnProvider;
        parent::__construct($scopeConfig, $storeManager, $collectionFactory, $resourceColFactory, $data);
        }
    
        /**
         * @inheritdoc
         * @throws \Exception
         */
        public function getAttributeCollection()
        {
        return $this->attributeCollectionProvider->get();
        }
    
        /**
         * @inheritdoc
         * @throws Exception 
         */
        public function export()
        {
    
    
        $writer = $this->getWriter();
        $writer->setHeaderCols($this->_getHeaderColumns());
    
        /** @var SourceItemCollection $collection */
        $collection = $this->sourceItemCollectionFactory->create(
            $this->getAttributeCollection(),
            $this->_parameters
        );
    
        $productCollection = $this->_addProductCollection($collection);
        $entityCollection = $this->_getEntityCollection(true);
            $entityCollection->setOrder('entity_id', 'asc');
            $entityCollection->setStoreId(Store::DEFAULT_STORE_ID);
    
    
    
        foreach ($productCollection->getData() as $data) {
    
            unset($data[SourceItem::ID_FIELD_NAME]);
             unset($data['name']);
            $writer->writeRow($data);
        }
    
        return $writer->getContents();
        }
    
        public function _addProductCollection($collection)
        {
    
         $collection->getSelect()->joinLeft(
            ['catalog_product' => 'catalog_product_entity'],
            '(main_table.sku = catalog_product.sku)',
            [
            'catalog_product.entity_id',
            'catalog_product.attribute_set_id',
            ]
        );
    
        return $collection;
        }
    
        /**
         * @inheritdoc
         * @throws Exception
         */
        protected function _getHeaderColumns()
        {
        return $this->columnProvider->getHeaders($this->getAttributeCollection(), $this->_parameters);
        }
    
        /**
         * @inheritdoc
         * @SuppressWarnings(PHPMD.UnusedFormalParameter)
         */
        public function exportItem($item)
        {
        // will not implement this method as it is legacy interface
        }
    
        public function getEntityTypeCode()
        {
        return  'custmom_stock_source';
        }
    
       /* protected function _getEntityCollection()
        {
        // TODO: Implement _getEntityCollection() method.
        }*/
    
    
        /**
         * Get entity collection
         *
         * @param bool $resetCollection
         * @return \Magento\Framework\Data\Collection\AbstractDb
         */
        protected function _getEntityCollection($resetCollection = false)
        {
        if ($resetCollection || empty($this->_entityCollection)) {
            $this->_entityCollection = $this->_entityCollectionFactory->create();
        }
        return $this->_entityCollection;
        }
    
    }
    
  3. Vendor/Module/Model/Export/AttributeCollectionProvider.php

      <?php
    
     declare(strict_types=1);
    
     namespace Vendor\Module\Model\Export;
    
     use Magento\Eav\Model\Entity\AttributeFactory;
     use Magento\Framework\Data\Collection;
     use Magento\ImportExport\Model\Export\Factory as CollectionFactory;
     use Magento\InventoryApi\Api\Data\SourceItemInterface;
     use Magento\InventoryImportExport\Model\Export\Source\StockStatus;
    
     /**
      * @api 
      */
     class AttributeCollectionProvider
     {
         /**
          * @var Collection
          */
         private $collection;
    
         /**
          * @var AttributeFactory
          */
         private $attributeFactory;
    
         /**
          * @param CollectionFactory $collectionFactory
          * @param AttributeFactory $attributeFactory
          * @throws \InvalidArgumentException
          */
         public function __construct(
         CollectionFactory $collectionFactory,
         AttributeFactory $attributeFactory
         ) {
         $this->collection = $collectionFactory->create(Collection::class);
         $this->attributeFactory = $attributeFactory;
         }
    
         /**
          * @return Collection
          * @throws \Exception
          */
         public function get(): Collection
         {
         if (count($this->collection) === 0) {
             /** @var \Magento\Eav\Model\Entity\Attribute $sourceCodeAttribute */
             $sourceCodeAttribute = $this->attributeFactory->create();
             $sourceCodeAttribute->setId(SourceItemInterface::SOURCE_CODE);
             $sourceCodeAttribute->setDefaultFrontendLabel(SourceItemInterface::SOURCE_CODE);
             $sourceCodeAttribute->setAttributeCode(SourceItemInterface::SOURCE_CODE);
             $sourceCodeAttribute->setBackendType('varchar');
             $this->collection->addItem($sourceCodeAttribute);
    
             /** @var \Magento\Eav\Model\Entity\Attribute $skuAttribute */
             $skuAttribute = $this->attributeFactory->create();
             $skuAttribute->setId(SourceItemInterface::SKU);
             $skuAttribute->setBackendType('varchar');
             $skuAttribute->setDefaultFrontendLabel(SourceItemInterface::SKU);
             $skuAttribute->setAttributeCode(SourceItemInterface::SKU);
             $this->collection->addItem($skuAttribute);
    
             /** @var \Magento\Eav\Model\Entity\Attribute $statusIdAttribute */
             $statusIdAttribute = $this->attributeFactory->create();
             $statusIdAttribute->setId(SourceItemInterface::STATUS);
             $statusIdAttribute->setDefaultFrontendLabel(SourceItemInterface::STATUS);
             $statusIdAttribute->setAttributeCode(SourceItemInterface::STATUS);
             $statusIdAttribute->setBackendType('int');
             $statusIdAttribute->setFrontendInput('select');
             $statusIdAttribute->setSourceModel(StockStatus::class);
             $this->collection->addItem($statusIdAttribute);
    
             /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */
             $quantityAttribute = $this->attributeFactory->create();
             $quantityAttribute->setId(SourceItemInterface::QUANTITY);
             $quantityAttribute->setBackendType('decimal');
             $quantityAttribute->setDefaultFrontendLabel(SourceItemInterface::QUANTITY);
             $quantityAttribute->setAttributeCode(SourceItemInterface::QUANTITY);
             $this->collection->addItem($quantityAttribute);
    
             /** @var \Magento\Eav\Model\Entity\Attribute $quantityAttribute */
             $productnameAttribute = $this->attributeFactory->create();
             $productnameAttribute->setId('name');
             $productnameAttribute->setBackendType('varchar');
             $productnameAttribute->setDefaultFrontendLabel('name');
             $productnameAttribute->setAttributeCode('name');
             $this->collection->addItem($productnameAttribute);
    
         }
    
         return $this->collection;
         }
     }
    

I am getting below error

 main.CRITICAL: Message has been rejected: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'main_table.name' in 'field list', query was: SELECT `main_table`.`source_item_id`, `main_table`.`source_code`, `main_table`.`sku`, `main_table`.`status`, `main_table`.`quantity`, `main_table`.`name`, `catalog_product`.`entity_id`, `catalog_product`.`attribute_set_id` FROM `inventory_source_item` AS `main_table`LEFT JOIN `catalog_product_entity` AS `catalog_product` ON (main_table.sku = catalog_product.sku) [] []

So export writer export data only from main table it is not using join table.

Kindly help.

Thanks Amit

0

Your Answer

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

Browse other questions tagged or ask your own question.