0

I have some differents users that will be use the admin, on this way, I need to create different rules to edit the products. On this way, I´ll create a rule that the user can edit just PRICES and STOCK, just this. I was following this exemple: How to disable product edit fields based on admin role in magento 2 , but I believe that I need to do more things. Can anyone help me with this, please?

1 Answer 1

1

According to what you were following you can do this.

I am assuming you have created a user, role, and assigned a role to the user. And new user only have catalog->product menu access only.

Now create a custom module.

[Vendor]/[Module]/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor]_[Module]',
    __DIR__
);

[Vendor]/[Module]/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[Vendor]_[Module]" setup_version="1.0.0" />
</config>

[Vendor]/[Module]/view/adminhtml/layout/catalog_product_edit.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="before.body.end">
          <block class="Magento\Framework\View\Element\Template" template="[Vendor]_[Module]::hideattribute.phtml" name="after"/> 
        </referenceContainer>
    </body>
</page>

[Vendor]/[Module]/view/adminhtml/templates/hideattribute.phtml

<?php
    $role_id = 3; //user's role id for example I have used "3"
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $adminSession = $objectManager->get('Magento\Backend\Model\Auth\Session');
    $current_adminuser =    $adminSession->getUser()->getRole()->getRoleId();
?>
<?php if($role_id == $current_adminuser): ?>
    <style>
[data-index = status],[data-index = attribute_set_id],[data-index = attribute_set_id],[data-index = name],[data-index = sku],[data-index = tax_class_id],[data-index = container_weight],[data-index = container_category_ids],[data-index = visibility],[data-index = container_news_from_date],[data-index = country_of_manufacture],[data-index = activity] {
        display: none;
    }
    </style>
<?php endif; ?>

now you have to keep adding [data-index="indexName"] where indexName is what you will find in inspect at product edit page. For example <div class="admin__field admin__field-small _required" data-bind="css: $data.additionalClasses, attr: {'data-index': index}, visible: visible" data-index="sku">

Above thing in css and user's role id that two thing you will have to do mainly.

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.