0

I'm using Magento 2.3 and I'm trying to add a column with an attribute inside the "products in category" grid (Catalog->Categories->Products in Category).
I've created a custom module and the file Product.php in

Mycompany\Mymodule\Block\Catalog\Adminhtml\Category\Tab


I'm pasting the code below:

<?php

namespace Mycompany\Mymodule\Block\Catalog\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()->addAttributeToSelect('manufacturer');

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

    return $grid;
}

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

    $this->addColumn(
        'manufacturer',
        [
            'header' => __('Manufacturer'),
            'name' => 'manufacturer',
            'index' => 'manufacturer',
        ]
    );


    return $this;
}
}

I'm attaching the screenshot of the result:
enter image description here

As you can see the column gets added and the attribute displayed, but with the id of the option instead of the text label.
How can I change my code so that it returns the attribute label (in this case woul be "test") instead of the value?
Any help is greatly appreciated, thanks!

  • check my answer – Rk Rathod Jun 20 at 15:53
0

Add Column Like This :-

$this->addColumn(
        'manufacturer',
        [
            'header' => __('Manufacturer'),
            'name' => 'manufacturer',
            'index' => 'manufacturer',
            'type'      =>'options',
            'options'   => $this->getManufacturer()
        ]
    );

And Create getManufacturer() Function for get Manufacturer Name :-

public function getManufacturer()
    {
        $manuf = $this->yourFactory->create();
        $test = array();
        foreach ($test->getData() as $testData) {
            $test[$testData['manufacturer_name']] = $testData['manufacturer_name'];
        }
        return $test;
    }
  • Thank you, I'm testing your answer but I don't have any factory set up. Do i need to use object manager for that? – Davide Bardelle Jun 21 at 7:16
  • in file create public function __construct(){ } and call your factory – Rk Rathod Jun 21 at 7:18

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.