Question

In this question https://magento.stackexchange.com/a/324446/92235, there is an excellent solution related to hiding the price from a specific user group. But this one does not hide the price from the grid, how can this be implemented? enter image description here

enter image description here

Was it helpful?

Solution

di.xml updated from previous question:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav">
        <plugin name="product_form_modifier" type="Mbs\HidePrice\Plugin\ProductAttributeModifier" />
    </type>
    <preference for="Magento\Catalog\Ui\Component\Listing\Columns" type="Mbs\HidePrice\Ui\Component\Listing\ProductGridColumns" />
</config>

preference as plugin is not possible on columns

<?php

namespace Mbs\HidePrice\Ui\Component\Listing;

class ProductGridColumns extends \Magento\Catalog\Ui\Component\Listing\Columns
{
    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Catalog\Ui\Component\ColumnFactory $columnFactory,
        \Magento\Catalog\Ui\Component\Listing\Attribute\RepositoryInterface $attributeRepository,
        \Mbs\HidePrice\Model\AuthorisationReader $authorization,
        array $components = [],
        array $data = []
    ) {
        if (!$authorization->canShowPriceInBackend()) {
            unset($components['price']);
        }
        parent::__construct($context, $columnFactory, $attributeRepository, $components, $data);
    }
}

I have done a small refactoring and now the authorisation is made in a separate model (see below):

class AuthorisationReader
{
    /**
     * @var \Magento\Framework\AuthorizationInterface
     */
    private $authorization;

    public function __construct(
        \Magento\Framework\AuthorizationInterface $authorization
    ) {
        $this->authorization = $authorization;
    }

    /**
     * @return bool
     */
    public function canShowPriceInBackend(): bool
    {
        return $this->authorization->isAllowed('Mbs_AdminScreen::Mbs_HidePrice::AccessPrice');
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top