How to remove delete product functionality for a specific user role? ( The user should only have access to edit the products. ) In default magento community edition i cannot achieve this. Which code should i modify to make this work?
1 Answer
Create a custom module. Make the necessary name changes and add this to the etc/config.xml. Rewriting adminthtml to remove the delete and mass delete. Events are to lock a particular attribute if you don't want to give the user role to edit that attribute.
<blocks>
<custom_catalog>
<class>Custom_Catalog_Block</class>
</custom_catalog>
<adminhtml>
<rewrite>
<catalog_product_edit>Custom_Catalog_Block_Adminhtml_Product_Edit</catalog_product_edit>
<catalog_product_grid>Custom_Catalog_Block_Adminhtml_Product_Grid</catalog_product_grid>
</rewrite>
</adminhtml>
</blocks>
<events>
<catalog_product_edit_action>
<observers>
<custom_catalog>
<type>singleton</type>
<class>custom_catalog/observer</class>
<method>lockAttributes</method>
</custom_catalog>
</observers>
</catalog_product_edit_action>
<catalog_product_new_action>
<observers>
<custom_catalog>
<type>singleton</type>
<class>custom_catalog/observer</class>
<method>lockAttributes</method>
</custom_catalog>
</observers>
</catalog_product_new_action>
</events>
Block/Adminhtml/Product/Edit.php (This is to remove the delete button in the product edit page)
class Custom_Catalog_Block_Adminhtml_Product_Edit extends Mage_Adminhtml_Block_Catalog_Product_Edit
{
public function getDeleteButtonHtml()
{
$currentUser = Mage::getSingleton('admin/session')->getUser();
$currentRole = $currentUser->getRole();
$roleId = $currentRole->getId();
//hardcoded the user role id for demo purposes
if($roleId == 1){
return $this->getChildHtml('delete_button');
}else{
return '';
}
}
}
Block/Adminhtml/Product/Grid.php (This is to remove the mass delete from actions in all products page)
class Custom_Catalog_Block_Adminhtml_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
protected function _prepareMassaction()
{
parent::_prepareMassaction();
$currentUser = Mage::getSingleton('admin/session')->getUser();
$currentRole = $currentUser->getRole();
$roleId = $currentRole->getId();
//hardcoded the user role id for demo purposes
if($roleId != 1){
//like this you can remove other actions as well. eg:- status
$this->getMassactionBlock()->removeItem('delete');
}
return $this;
}
}
Model/Observer.php (To lock attributes in product edit page if required)
class Custom_Catalog_Model_Observer {
public function lockAttributes($observer) {
$event = $observer->getEvent();
$product = $event->getProduct();
$currentUser = Mage::getSingleton('admin/session')->getUser();
$currentRole = $currentUser->getRole();
$roleId = $currentRole->getId();
//hardcoded the user role id for demo purposes
if($roleId != 1){
//like this you can lock any attribute you like in product edit
$product->lockAttribute('price');
$product->lockAttribute('status');
}
}
}
-
I tried this way but nothing happens user still have the DELETE button Commented Nov 28, 2017 at 9:22
-
//hardcoded the user role id for demo purposes if($roleId != 1){ //like this you can remove other actions as well. eg:- status $this->getMassactionBlock()->removeItem('delete'); }
You need to change the roleid according to the user role that you want to hide it. Commented Nov 29, 2017 at 6:52 -
i created a new user whose id is 2, and i changed in Edit.php Commented Nov 29, 2017 at 9:05