0

I have an improvement problem; I want to restrict some more things from that user. I want to block "delete" the product

Magento 2.4 How to disable/hide "Delete" from Catalog Products Actions

Ver.: Magento 2.4.4

enter image description here

2 Answers 2

0

Magento 2 used UI Component to render products grid at admin, you can remove delete action at: magento\module-catalog\view\adminhtml\ui_component\product_listing.xml:

<action name="delete">
<settings>
    <confirm>
        <message translate="true">Delete selected items? 
    </message>
        <title translate="true">Delete items</title>
    </confirm>
    <url path="catalog/product/massDelete"/>
    <type>delete</type>
    <label translate="true">Delete</label>
</settings>

and you can restrict delete action for specific user by create a after plugin for "Magento\Catalog\Ui\Component\Product\MassAction" class "isActionAllowed()" method.

Add plugin configuration in etc/adminhtml/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\Component\Product\MassAction">
    <plugin name="restrict_delete_product" type="Vendor\Module\Plugin\MassAction" sortOrder="1" disabled="false"/>
</type>
    <?php

namespace Vendor\Module\Plugin;

use Magento\Catalog\Ui\Component\Product\MassAction as ProductMassAction;
use Magento\Framework\AuthorizationInterface;

class MassAction
{
    /**
     * @var AuthorizationInterface
     */
    private $authorization;

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

    public function afterIsActionAllowed(ProductMassAction $subject, $result, $actionType)
    {
        $isAllow = $this->authorization->isAllowed("Vendor_Module::resource_id");
        if (($actionType == "delete") && (!$isAllow)) {
            return false;
        }
        return $result;
    }
}
0
  1. Create di.xml on path app\code\Vendor\Extension\etc\adminhtml\
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Catalog\Ui\Component\Product\MassAction">
    <plugin name="hide_delete_from_catalog_massaction" type="Vendor\Extension\Plugin\Catalog\Ui\Component\Product\MassAction" sortOrder="1"/>
</type>
</config>
  1. Then, create a “MassAction.php” file on path app\code\Vendor\Extension\Plugin\Catalog\Ui\Component\Product\

<?php
namespace Vendor\Extension\Plugin\Catalog\Ui\Component\Product;

 class MassAction
    {
        public function afterIsActionAllowed(
            \Magento\Catalog\Ui\Component\Product\MassAction $subject,
            $isAllowed,
            $actionType)
        {
            if ($actionType == 'delete')
            {
                return false;
            }
            return $isAllowed;
        }
    }

"Delete" from Catalog Products Actions

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.