0

I want to add a column of a newly created boolean attribute, but it doesn't work, I have read blogs where they give the same advice but it hasn't worked

enter image description here

I have followed the following steps:

Create boolean attribute:

enter image description here

Extend the Product class from the vendor

app\code\Vendor\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="Vendor\Module\Block\Adminhtml\Category\Tab\Product"/>
    </config>

app\code\Vendor\Module\Block\Adminhtml\Category\Tab\Product.php

<?php

namespace Vendor\Module\Block\Adminhtml\Category\Tab;

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Framework\App\ObjectManager;
use Magento\Eav\Model\Config;

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

    public function __construct(\Magento\Backend\Block\Template\Context $context,
                                \Magento\Backend\Helper\Data $backendHelper,
                                \Magento\Catalog\Model\ProductFactory $productFactory,
                                \Magento\Framework\Registry $coreRegistry,
                                array $data = [],
                                Visibility $visibility = null,
                                Status $status = null,
                                Config $eavConfig)
    {
        $this->eavConfig = $eavConfig;
        $this->visibility = $visibility ?: ObjectManager::getInstance()->get(Visibility::class);
        parent::__construct($context, $backendHelper, $productFactory, $coreRegistry, $data, $visibility, $status);
    }

    public function setCollection($collection)
    {
        $collection->addAttributeToSelect('destacado_categoria');
        parent::setCollection($collection);
    }

    protected function _prepareColumns()
    {
        $attribute = $this->eavConfig->getAttribute('catalog_product', 'destacado_categoria');
        if ($attribute)
        {
            $vals = $attribute->getSource()->getAllOptions();
            $arr = [];
            foreach ($vals as $option)
            {
                if ($option['label'])
                {
                    $arr[$option['value']] = $option['label'];
                }
            }
            parent::_prepareColumns();
            $this->addColumnAfter('destacado_categoria', array(
                'header' => __('Destacado Categoría'),
                'index' => 'destacado_categoria',
                'type' => 'options',
                'options' => $arr,
            ), 'sku');

            $this->sortColumnsByOrder();
            return $this;
        }
    }
}

I need a special command line to be able to visualize it?

Please help, thanks!

Your Answer

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

Browse other questions tagged or ask your own question.