2

how to implement functionality that will hide product prices from the user group. I've seen such a feature in an entrepreneurial Magento, but I can't figure out how to implement such a functionality. enter image description here

enter image description here

1

great question. The acl in Magento allows any customisation (if not coming out of the box) but indeed we have to do it directly interacting with the code as below:

I have added a plugin and an acl resource, it is straightforward as you will see but quite deep in the code. good luck with this.

acl.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
    <acl>
        <resources>
            <resource id="Magento_Backend::admin">
                <resource id="Magento_Catalog::catalog">
                    <resource id="Magento_Catalog::catalog_inventory">
                        <resource id="Magento_Catalog::products">
                            <resource id="Magento_Catalog::update_attributes">
                                <resource id="Mbs_HidePrice::AccessPrice" title="Access Price" translate="title" sortOrder="10" />
                            </resource>
                        </resource>
                    </resource>
                </resource>
            </resource>
        </resources>
    </acl>
</config>

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">
    <type name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav">
        <plugin name="product_form_modifier" type="Mbs\HidePrice\Plugin\ProductAttributeModifier" />
    </type>
</config>

and finally, the plugin

<?php

namespace Mbs\HidePrice\Plugin;

use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;

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

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

    public function afterModifyMeta(
        \Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Eav $subject,
        array $meta
    ) {
        $parsedMeta = [];

        foreach ($meta as $groupCode => $group) {
            if ($groupCode === AbstractModifier::DEFAULT_GENERAL_PANEL) {
                $attributes = $group['children'];

                $newAttributes = [];
                foreach ($attributes as $key => $attribute) {
                    if (preg_match('%'. AbstractModifier::CONTAINER_PREFIX .'(.+)%', $key, $m)) {
                        $attributeCode = $m[1];
                        if ($attributeCode !== 'price') {
                            $newAttributes[] = $attribute;
                        } else if ($this->canShowPriceInBackend()) {
                            $newAttributes[] = $attribute;
                        }
                    }
                }

                $group['children'] = $newAttributes;
                $parsedMeta[$groupCode] = $group;
            } else {
                $parsedMeta[$groupCode] = $group;
            }
        }

        return $parsedMeta;
    }

    /**
     * @return bool
     */
    private function canShowPriceInBackend(): bool
    {
        return $this->authorization->isAllowed('Mbs_AdminScreen::Mbs_HidePrice::AccessPrice');
    }
}
| improve this answer | |
  • This code works great, thanks for your example, but can you please tell me how to disable the display from the grid? When entering the product, there is no price, but the grid displays – Sdfhbbh Hbcgnvv Oct 15 at 17:10
  • Your question does ask for the form, you may accept and write another question if you want to rely on me. In fact, the grid may involve a bounty I guess – Herve Tribouilloy Oct 15 at 17:25
  • @HerveTribouilloy Hello, It seems to me that the attribute is inserted in the wrong place. It is not affected by validation and so on. For example, price is not blocked on configurable products if there is at least 1 option. Advanced Pricing has been moved to the left of the page. How can this be fixed? – Alice Oct 21 at 10:30
  • Can you put details of your issue @Alice, I am happy to look at your specific setup and it may be that I have overlooked a use case with it – Herve Tribouilloy Oct 25 at 12:04

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.