0

Magento 2.2.3

The columns in 2.2.3 are very limited, I need the SKU and the status and maybe some more columns. Is there a way to add columns to the "products in category" grid in a category?

Thanks!

0
1

VendorNameSpace/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="Magento\Catalog\Block\Adminhtml\Category\Tab\Product" type="VendorNameSpace\Module\Block\Adminhtml\Category\Tab\Product"/>
</config>

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

<?php
namespace VendorNameSpace\Module\Block\Adminhtml\Category\Tab;

use Magento\Backend\Block\Widget\Grid\Extended;
use Magento\Backend\Block\Widget\Grid;

class Product extends \Magento\Catalog\Block\Adminhtml\Category\Tab\Product
{
    /**
     * @return Grid
     */
    protected function _prepareCollection()
    {
        $grid = parent::_prepareCollection();

        $collection = $this->getCollection();
        $collection->joinTable(
            'cataloginventory_stock_status',
            'product_id=entity_id',
            ["new_attribute" => "new_attribute"],
            null ,
            'left'
        )->addAttributeToSelect('new_attribute');

        $this->setCollection($collection);
        if ($this->getCollection()->isLoaded()) {
            $this->getCollection()->clear();
        }
        return $grid;
    }

    /**
     * @return Extended
     */
    protected function _prepareColumns()
    {
        parent::_prepareColumns();

        $this->addColumn('new_attribute', ['header' => __('Stock Status'), 'index' => 'new_attribute']);

        return $this;
    }
}

helps this achieve your requirement. thanks

2
  • Thanks! How could I get the status (enabled/disabled) here?
    – Arnie
    Oct 9 '19 at 8:39
  • How to add New column attribute and save it in to database Feb 11 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.