1

I would like to implement export catalog products functionality from admin end from custom module.

I have followed below steps for displaying export button at admin end catalog > products but unable to export products data in the form of csv/xml/xls.

  1. Created custom module Vendor_Employee.

  2. app/code/Vendor/Employee/view/adminhtml/ui_component/product_listing.xml

    <?xml version="1.0" encoding="UTF-8"?>       
    <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">product_listing.product_listing_data_source</item>
        </item>
    </argument>
    <settings>
        <spinner>product_columns</spinner>
        <deps>
            <dep>product_listing.product_listing_data_source</dep>
        </deps>
    </settings>
    <dataSource name="product_listing_data_source" component="Magento_Ui/js/grid/provider">
        <settings>
            <storageConfig>
                <param name="dataScope" xsi:type="string">filters.store_id</param>
            </storageConfig>
            <updateUrl path="mui/index/render"/>
        </settings>
        <aclResource>Magento_Catalog::products</aclResource>
        <dataProvider class="Magento\Catalog\Ui\DataProvider\Product\ProductDataProvider" name="product_listing_data_source">
            <settings>
                <requestFieldName>id</requestFieldName>
                <primaryFieldName>entity_id</primaryFieldName>
            </settings>
        </dataProvider>
    </dataSource>
    <listingToolbar name="listing_top">
        <settings>
            <sticky>true</sticky>
        </settings>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <filters name="listing_filters">
            <filterSelect name="store_id" provider="${ $.parentName }">
                <settings>
                    <options class="Magento\Store\Ui\Component\Listing\Column\Store\Options"/>
                    <caption translate="true">All Store Views</caption>
                    <label translate="true">Store View</label>
                    <dataScope>store_id</dataScope>
                </settings>
            </filterSelect>
        </filters>
        <massaction name="listing_massaction"
                    component="Magento_Ui/js/grid/tree-massactions"
                    class="\Magento\Catalog\Ui\Component\Product\MassAction">
            <action name="delete">
                <settings>
                    <confirm>
                        <message translate="true">Delete selected items?</message>
                        <title translate="true">Delete items</title>
                    </confirm>
                    <url path="catalog/product/massDelete"/>
                    <type>delete</type>
                    <label translate="true">Delete1</label>
                </settings>
            </action>
            <action name="status">
                <settings>
                    <type>status</type>
                    <label translate="true">Change status1</label>
                    <actions>
                        <action name="0">
                            <type>enable</type>
                            <label translate="true">Enable</label>
                            <url path="catalog/product/massStatus">
                                <param name="status">1</param>
                            </url>
                        </action>
                        <action name="1">
                            <type>disable</type>
                            <label translate="true">Disable1</label>
                            <url path="catalog/product/massStatus">
                                <param name="status">2</param>
                            </url>
                        </action>
                    </actions>
                </settings>
            </action>
            <action name="attributes">
                <settings>
                    <url path="catalog/product_action_attribute/edit"/>
                    <type>attributes</type>
                    <label translate="true">Update attributes</label>
                </settings>
            </action>
        </massaction>
        <paging name="listing_paging"/>
    
        <!-- Added Export Button Here -->
    
        <exportButton class="Magento\Ui\Component\ExportButton">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="options" xsi:type="array">
                        <item name="cvs" xsi:type="array">
                            <item name="value" xsi:type="string">csv</item>
                            <item name="label" xsi:type="string" translate="true">CSV</item>
                            <item name="url" xsi:type="string">employee/export/gridToCsv</item>
                        </item>
                        <item name="xml" xsi:type="array">
                            <item name="value" xsi:type="string">xml</item>
                            <item name="label" xsi:type="string" translate="true">Excel XML</item>
                            <item name="url" xsi:type="string">employee/export/gridToXml</item>
                        </item>
                        <item name="xls" xsi:type="array">
                            <item name="value" xsi:type="string">xls</item>
                            <item name="label" xsi:type="string" translate="true">Excel XLS</item>
                            <item name="url" xsi:type="string">employee/export/gridToXls</item>
                        </item>
                    </item>
                </item>
            </argument>
        </exportButton>
     <!-- Added Export Button Here -->
    
    </listingToolbar>
    <columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <settings>
            <childDefaults>
                <param name="fieldAction" xsi:type="array">
                    <item name="provider" xsi:type="string">product_listing.product_listing.product_columns.actions</item>
                    <item name="target" xsi:type="string">applyAction</item>
                    <item name="params" xsi:type="array">
                        <item name="0" xsi:type="string">edit</item>
                        <item name="1" xsi:type="string">${ $.$data.rowIndex }</item>
                    </item>
                </param>
            </childDefaults>
        </settings>
        <selectionsColumn name="ids" sortOrder="0">
            <settings>
                <indexField>entity_id</indexField>
            </settings>
        </selectionsColumn>
        <column name="entity_id" sortOrder="10">
            <settings>
                <filter>textRange</filter>
                <label translate="true">ID</label>
                <sorting>asc</sorting>
            </settings>
        </column>
        <column name="thumbnail" class="Magento\Catalog\Ui\Component\Listing\Columns\Thumbnail" component="Magento_Ui/js/grid/columns/thumbnail" sortOrder="20">
            <settings>
                <altField>name</altField>
                <hasPreview>1</hasPreview>
                <addField>true</addField>
                <label translate="true">Thumbnail</label>
                <sortable>false</sortable>
            </settings>
        </column>
        <column name="name" sortOrder="30">
            <settings>
                <addField>true</addField>
                <filter>text</filter>
                <label translate="true">Name</label>
            </settings>
        </column>
        <column name="type_id" component="Magento_Ui/js/grid/columns/select" sortOrder="40">
            <settings>
                <options class="Magento\Catalog\Model\Product\Type"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Type</label>
            </settings>
        </column>
        <column name="attribute_set_id" component="Magento_Ui/js/grid/columns/select" sortOrder="50">
            <settings>
                <options class="Magento\Catalog\Model\Product\AttributeSet\Options"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Attribute Set</label>
            </settings>
        </column>
        <column name="sku" sortOrder="60">
            <settings>
                <filter>text</filter>
                <label translate="true">SKU</label>
            </settings>
        </column>
        <column name="price" class="Magento\Catalog\Ui\Component\Listing\Columns\Price" sortOrder="70">
            <settings>
                <addField>true</addField>
                <filter>textRange</filter>
                <label translate="true">Price</label>
            </settings>
        </column>
        <column name="visibility" component="Magento_Ui/js/grid/columns/select" sortOrder="80">
            <settings>
                <addField>true</addField>
                <options class="Magento\Catalog\Model\Product\Visibility"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Visibility</label>
            </settings>
        </column>
        <column name="status" component="Magento_Ui/js/grid/columns/select" sortOrder="90">
            <settings>
                <addField>true</addField>
                <options class="Magento\Catalog\Model\Product\Attribute\Source\Status"/>
                <filter>select</filter>
                <dataType>select</dataType>
                <label translate="true">Status</label>
            </settings>
        </column>
        <column name="websites" class="Magento\Catalog\Ui\Component\Listing\Columns\Websites" sortOrder="100">
            <settings>
                <addField>true</addField>
                <options class="Magento\Store\Model\ResourceModel\Website\Collection"/>
                <dataType>text</dataType>
                <label translate="true">Websites</label>
            </settings>
        </column>
        <actionsColumn name="actions" class="Magento\Catalog\Ui\Component\Listing\Columns\ProductActions" sortOrder="200">
            <settings>
                <indexField>entity_id</indexField>
            </settings>
        </actionsColumn>
    </columns>
    

  3. app/code/Vendor/Employee/Controller/Adminhtml/Export/GridToCsv.php

    <?php
    namespace Vendor\Employee\Controller\Adminhtml\Export;
    use Magento\Backend\App\Action;
    use Magento\Backend\App\Action\Context;
    use Magento\Framework\App\ResponseInterface;
    use Magento\Framework\App\Filesystem\DirectoryList;
    use Magento\Framework\App\Response\Http\FileFactory;
    class gridToCsv extends Action
    {
    
    public function execute()
    {
            //csv
            $this->_view->loadLayout(false);
            $fileName = 'catalog_products.csv';
            $exportBlock = $this->_view->getLayout()->createBlock('Magento\Catalog\Block\Adminhtml\Product\Grid');
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $this->_fileFactory = $objectManager->create('Magento\Framework\App\Response\Http\FileFactory');
            return $this->_fileFactory->create(
                $fileName,
                $exportBlock->getCsvFile(),
                DirectoryList::VAR_DIR
            );
    
    }
    }
    

enter image description here

Here i am able to export only few columns but not all Please advise?

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.