7

I want to remove status column from Magento admin panel order grid.

I use below code to remove the status column in my sales_order_grid.xml file. Reference

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="status">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="componentDisabled" xsi:type="boolean">true</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

It removes status column successfully but I want to remove it based on a condition, value of a configuration field from Stores > Configuration section.

Is there any way I can add a condition on componentDisabled, or any other way to achieve my requirements?

6

It Might be helpful to you.

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="websites" class="Magento\Catalog\Ui\Component\Listing\Columns\Websites" sortOrder="100">
            <settings>
                <addField>true</addField>
                <options class="Magento\Store\Model\ResourceModel\Website\Collection"/>
                <dataType>text</dataType>
                <label translate="true">Websites</label>
            </settings>
        </column>
    </columns>
</listing>

Websites.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Catalog\Ui\Component\Listing\Columns;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
 * @api
 * @since 100.0.2
 */
class Websites extends \Magento\Ui\Component\Listing\Columns\Column
{
    /**
     * Column name
     */
    const NAME = 'websites';

    /**
     * Store manager
     *
     * @var StoreManagerInterface
     */
    protected $storeManager;

    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param StoreManagerInterface $storeManager
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        StoreManagerInterface $storeManager,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->storeManager = $storeManager;
    }

    /**
     * {@inheritdoc}
     * @deprecated 101.0.0
     */
    public function prepareDataSource(array $dataSource)
    {
        $websiteNames = [];
        foreach ($this->getData('options') as $website) {
            $websiteNames[$website->getWebsiteId()] = $website->getName();
        }
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item) {
                $websites = [];
                foreach ($item[$fieldName] as $websiteId) {
                    if (!isset($websiteNames[$websiteId])) {
                        continue;
                    }
                    $websites[] = $websiteNames[$websiteId];
                }
                $item[$fieldName] = implode(', ', $websites);
            }
        }

        return $dataSource;
    }

    /**
     * Prepare component configuration
     * @return void
     */
    public function prepare()
    {
        parent::prepare();
        if ($this->storeManager->isSingleStoreMode()) {
            $this->_data['config']['componentDisabled'] = true;
        }
    }
}

It's defualt Magento which is used for to display website based on condition.

  • Thanks Dharmendra for the answer. I will try to implement it with my code. – Jaimin Sutariya Sep 18 '18 at 11:22
  • Welcome bro anytime. – Dharmendra Jadav Sep 18 '18 at 11:38
  • @DharmendraJadav Can we able to hide column bases on admin logged in USER? I want to hide some column from admin grid using UI Component. Want to show some column for some user and rest of the user can able to see all columns – Kartik Asodariya Jun 13 at 5:51
  • @Kartik we need to check for that. because i haven't check that before. But yes we can do that. Not impossible – Dharmendra Jadav Jun 13 at 5:58
  • @DharmendraJadav can you please post here so will be helping to me as well others. Your efforts will be appreciated. – Kartik Asodariya Jun 13 at 6:08

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.