Skip to content

Overriding MagentoCatalogBlockAdminhtmlCategoryTabProduct

Magento 2.4

I have a need to display a custom product attribute called ‘promotion_on’ in [Backend > Catalog > Categories > {Category from tree } -> Products in Category ] Products grid as an additional column.

I tried doing it with a module with following files. However expected overriding does not happen as it seems. Templatehints are also showing original paths, not the custom module. Can someone please guide me how to resolve this.

Vendor/Module/registration.php

<?php

MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Vendor/Module/etc/module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Vendor/Module/etc/adminhtml/di.xml

<?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="MagentoCatalogBlockAdminhtmlCategoryTabProduct" type="VendorModuleBlockAdminhtmlCategoryTabProduct"/>
</config>

Vendor/Module/Block/Adminhtml/Category/Tab/Product.php

<?php
namespace VendorModuleBlockAdminhtmlCategoryTab;

class Product extends MagentoCatalogBlockAdminhtmlCategoryTabProduct
{
    /**
     * Set collection object
     *
     * @param MagentoFrameworkDataCollection $collection
     * @return void
     */
    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('promotion_on');
        parent::setCollection($collection);
    }

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

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

Thanks and Best Regards

Indunil