How To

Magento 2: Create Separate Column for Additional Attribute While Export Product

Hello Magento Friends,

In today’s blog, we will learn about How to Create a Separate Column for Additional Attributes while exporting Products in Magento 2.

Magento 2 provides a comprehensive product export functionality, allowing store owners to export product information in CSV format. However, if you have custom attributes that are not included in the default export, you might find it challenging to include them in the exported file. Creating a separate column for these additional attributes can greatly enhance the flexibility of your product data management.

Let’s find out How to Create a Separate Column for Additional Attribute while Exporting Products in Magento 2.

Steps to Create Separate Column for Additional Attribute While Export Product in Magento 2:

Step 1: First, we need to create a “di.xml“ file inside our extension at the following path

app\code\Vendor\Extension\etc\di.xml

Then add the code as follows

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogImportExport\Model\Export\Product" type="Vendor\Extension\Model\Rewrite\CatalogImportExport\Export\Product" />
</config>

Step 2: After that, we need to create a “Product.php” file inside our extension at the following path

app\code\Vendor\Extension\Model\Rewrite\CatalogImportExport\Export\Product.php

And add the code as given below

<?php
namespace Vendor\Extension\Model\Rewrite\CatalogImportExport\Export;

class Product extends \Magento\CatalogImportExport\Model\Export\Product
{
    protected function setHeaderColumns($customOptionsData, $stockItemRows)
    {
        $attr = "Here Pass Attribute Code Array";
        $merge = explode(',', $attr);
  
        if (!$this->_headerColumns) {
            $customOptCols = [
                'custom_options',
            ];
            $this->_headerColumns = array_merge(
                [
                    self::COL_SKU,
                    self::COL_STORE,
                    self::COL_ATTR_SET,
                    self::COL_TYPE,
                    self::COL_CATEGORY,
                    self::COL_PRODUCT_WEBSITES,
                ],
                $this->_getExportMainAttrCodes(),
                $merge,
                [self::COL_ADDITIONAL_ATTRIBUTES],
                reset($stockItemRows) ? array_keys(end($stockItemRows)) : [],
                [],
                [
                    'related_skus',
                    'related_position',
                    'crosssell_skus',
                    'crosssell_position',
                    'upsell_skus',
                    'upsell_position'
                ],
                ['additional_images', 'additional_image_labels', 'hide_from_product_page']
            );
            if ($customOptionsData) {
                $this->_headerColumns = array_merge($this->_headerColumns, $customOptCols);
            }
        }
    }
}

Conclusion:

This way you can create a separate column for additional attributes while exporting products in Magento 2. If you have any doubts regarding the above steps, share them with me through the comment section. Share the solution with your friends and stay updated with us for more such Magento 2 tutorials.

Hire Experienced Magento Developers for customization requirements for your store.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

Mastering Tailwind CSS in Laravel: A Comprehensive Guide

Tailwind CSS has emerged as a powerful utility-first CSS framework, offering developers a unique approach…

20 hours ago

React Native or Flutter in 2024

The mobile app development field has witnessed a rapid revolution over the past few years.…

3 days ago

Magento 2: How To Call JS on the Checkout Page?

Hello Magento mates, Today we will learn to add a call JS on the checkout…

6 days ago

Boost Your SEM Game: Unveiling the Top 10 Tools for Marketers in 2024

Business survival in today’s digital world has become extremely difficult. Using traditional marketing techniques is…

7 days ago

Five Essential Payroll Compliance Tips for eCommerce Startups

Are you setting up a payroll system for your eCommerce startup? Ensuring compliance with myriad…

1 week ago

Optimizing Laravel Blade: Unlocking Advanced Fetcher Techniques

In the expansive universe of Laravel development, Blade serves as the stellar templating engine, propelling…

1 week ago