Add Quantity to Product Widget Grid in Category Edit in Magento 2

Event/Observer

Event: backend_block_widget_grid_prepare_grid_before

Create Module

Namespace\Module\registration.php
Namespace\Module\composer.json
Namespace\Module\etc\module.xml
Namespace\Module\etc\adminhtml\events.xml
Namespace\Module\Model\Observer\BackendBlockWidgetGridPrepareGrid.php

events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="backend_block_widget_grid_prepare_grid_before">
        <observer name="category_product_picker_grid"
                  instance="Namespace\Module\Model\Observer\BackendBlockWidgetGridPrepareGrid"
        />
    </event>
</config>

BackendBlockWidgetGridPrepareGrid.php

namespace Namespace\Module\Model\Observer;

use Magento\Framework\Event\ObserverInterface;

class BackendBlockWidgetGridPrepareGrid implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $grid = $observer->getEvent()->getGrid();
        $grid->addColumn(
            'qty',
            [
                'header' => __('Quantity'),
                'type' => 'number',
                'index' => 'stock'
            ]
        );
        return $this;
    }
}

Leave a Reply

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