4

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!

2
  • check my answer Jun 20, 2019 at 15:53
  • @Davide , have you got the solution for above , if so can you share complete snippet Feb 11, 2021 at 10:57

2 Answers 2

1

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;
    }
3
  • 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? Jun 21, 2019 at 7:16
  • in file create public function __construct(){ } and call your factory Jun 21, 2019 at 7:18
  • @RkRathod , Hi i have added filed to grid but unable to save and retrieve the data in the field. Can you explain how to do that ? Feb 11, 2021 at 11:26
0

Ronak Rathod's answer is correct. Let me improve it, so that it will be helpful for others too

in module di.xml add this entry

<preference for="Magento\Catalog\Block\Adminhtml\Category\Tab\Product" type="Vendor\Module\Block\Adminhtml\Category\Tab\Product"/>

Now use this code in your overrided file:

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

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;

class Product extends \Magento\Catalog\Block\Adminhtml\Category\Tab\Product
{
    protected $attributeRepository;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Backend\Helper\Data $backendHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Registry $coreRegistry,
        \Magento\Catalog\Model\Product\Attribute\Repository $attributeRepository,
        array $data = [],
        Visibility $visibility = null,
        Status $status = null
    )
    {
        $this->attributeRepository = $attributeRepository;
        parent::__construct($context, $backendHelper, $productFactory, $coreRegistry, $data, $visibility, $status);
    }

    /**
     * @return \Magento\Backend\Block\Widget\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 \Magento\Backend\Block\Widget\Grid\Extended
     */
    protected function _prepareColumns()
    {
        parent::_prepareColumns();

        try {
            $this->addColumn(
                'manufacturer',
                [
                    'header' => __('Manufacturer'),
                    'name' => 'manufacturer',
                    'index' => 'manufacturer',
                    'type' => 'options',
                    'options'   => $this->getBrandsOptionArray()
                ]
            );
        }
        catch (\Exception $e){
            //Do nothing
        }

        return $this;
    }

    public function getBrandsOptionArray()
    {
        $options = $this->attributeRepository->get("mgs_brand")->getOptions();
        $optionsArray = [];
        foreach ($options as $option){
            if($option->getValue() == 0){
                continue;
            }
            $optionsArray[$option->getValue()] = $option->getLabel();
        }

        asort($optionsArray);
        return ($optionsArray);
    }
}

I have changed the function getBrandsOptionArray to get manufacturer options

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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