In this blog post, we discussed adding the custom product attribute in the category page of admin in Magento 2. In the last post, we have explained how to import product data programmatically in magento-2.
The default Magento 2 permits adding columns of the product attribute. you can use the below code which is useful for optimized backend performance. Now let’s see the following steps:
Step-1 Create di.xml file :
File: app\code\Webiators\AdminCategoryProduct\etc\di.xml
1 2 3 4 5 |
<?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="Webiators\AdminCategoryProduct\Block\Adminhtml\Category\Tab\Product" /> </config> |
Step-2 Create Product.php file :
File: app/code/Webiators/AdminCategoryProduct/Block/Adminhtml/Category/Tab/Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php declare(strict_types=1); namespace Webiators\AdminCategoryProduct\Block\Adminhtml\Category\Tab; use Magento\Framework\Data\Collection; class Product extends \Magento\Catalog\Block\Adminhtml\Category\Tab\Product { /** * Set collection object adding product url_key * * @param Collection $collection * @return void */ public function setCollection($collection) { $collection->addAttributeToSelect('url_key') $this->_collection = $collection; } /** * @return Product */ protected function _prepareColumns(): Product { parent::_prepareColumns(); $this->addColumnAfter( 'url_key', [ 'header' => __('Url Key'), 'index' => 'url_key' ], 'entity_id' ); $this->sortColumnsByOrder(); return $this; } } |
That’s all about add custom product attributes in the category page of admin in Magento 2.
If you want to download this extension from Github, check here.
Any questions about this topic please mention them in the comment section below.
Thank you.