Magento 2: How to Add a Column to Products in Category Grid

How to Add a Column to Products in Category Grid M2

Hello Magento Friends,

When managing products in Magento 2, having the ability to customize the admin grids can significantly enhance your workflow. One such grid is the category products grid, which appears under Catalog > Categories in the admin panel. This grid displays the list of products assigned to a specific category.

Magento version upgrade

The default grid contains the following columns:

Sometimes, store admins may require additional product attributes (like SKU, stock status, or manufacturer) to appear in this grid for quicker decisions or updates. Fortunately, Magento 2 allows you to add custom columns to this grid programmatically.

In this blog, we’ll walk you through how to add a custom column to the Category Products Grid in the Magento 2 admin.

Steps to Add a Column to Products in Category Grid in Magento 2:

Step 1: Create a di.xml file at the given path

app/code/Vendor/Extension/etc/adminhtml/di.xml

Now add the code as follows:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
     <preference for="Magento\Catalog\Block\Adminhtml\Category\Tab\Product" type="Vendor\Extension\Block\Adminhtml\Category\Tab\Product"/>
</config>

Step 2: Create a Product.php file at the given path

app/code/Vendor/Extension/Block/Adminhtml/Category/Tab/Product.php

Now add the code as follows:

<?php
namespace Vendor\Extension\Block\Adminhtml\Category\Tab;

class Product extends \Magento\Catalog\Block\Adminhtml\Category\Tab\Product
{
    /**
     * Set collection object
     *
     * @param \Magento\Framework\Data\Collection $collection
     * @return void
     */
    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('created_at');
        parent::setCollection($collection);
    }

    /**
     * @return $this
     */
    protected function _prepareColumns()
    {
        parent::_prepareColumns();
        $this->addColumnAfter('created_at', array(
            'header' => __('Created At'),
            'index' => 'created_at',
        ), 'sku');

        $this->sortColumnsByOrder();
        return $this;
    }
}

Output:

Conclusion:

Adding custom columns to Magento 2’s Category Products Grid can enhance the admin experience by giving quick access to important product attributes.

Hire Magento Programmer

Happy Coding!

FAQ

  1. What is the Category Products Grid in Magento 2?

Under Catalog > Categories in Magento 2, admin users can view, filter, and manage the list of products assigned to a particular category from the backend using the Category Products Grid.

  1. What is the purpose of adding a custom column to the Category Products Grid?

You may wish to add a custom column that displays additional product information, such as SKU, manufacturer, stock status, custom attributes, etc., to improve product management and visibility.

  1. Will frontend or display performance be impacted by the custom column?

No, adding a column to the admin Category Products Grid only affects the backend user experience; the frontend user experience remains unchanged. If the data is light and appropriately indexed, performance is rarely impacted.

Previous Article

Write Cleaner Laravel Code Using the when() Helper

Next Article

Laravel Tinker: Quick and Easy Command Line Interactions

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨