1

On Magento 2, and product page, there's a list of product type,

List of type products

I want to hide and disable and block a user to access an additional type of product,

for example: hide Configurable product and Bundle product and block the routes of it ( so, you can't add a configurable if you using the URL directly )

1

You can override the class \Magento\Catalog\Block\Adminhtml\Product using di.xml preference as describe below.

Assume you are using a custom module name "Company_MyModule"

step 1: create di.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/adminhtml

File: YOUR-MAGENTO-ROOT/app/code/Company/MyModule/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">
    <preference for="Magento\Catalog\Block\Adminhtml\Product" type="Company\MyModule\Block\Adminhtml\Product" />                
</config>

step 2: Create the class Product.php under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Block/Adminhtml

File : YOUR-MAGENTO-ROOT/app/code/Company/MyModule/Block/Adminhtml/Product.php

<?php

namespace Company\MyModule\Block\Adminhtml;

class Product extends \Magento\Catalog\Block\Adminhtml\Product
{
     protected function _getAddProductButtonOptions()
    {
        /* var Array $arrAlowedTypes */     
        $arrAlowedTypesIds = array('simple');
        $splitButtonOptions = [];
        $types = $this->_typeFactory->create()->getTypes();
        uasort(
            $types,
            function ($elementOne, $elementTwo) {
                return ($elementOne['sort_order'] < $elementTwo['sort_order']) ? -1 : 1;
            }
        );

        foreach ($types as $typeId => $type) {
            if(in_array($typeId,$arrAlowedTypesIds)) {              
             $splitButtonOptions[$typeId] = [
                'label' => __($type['label']),
                'onclick' => "setLocation('" . $this->_getProductCreateUrl($typeId) . "')",
                'default' => \Magento\Catalog\Model\Product\Type::DEFAULT_TYPE == $typeId,
             ];
          }
        }

        return $splitButtonOptions;
    }
}

Step 3: Run DI compile

sudo php bin/magento setup:di:compile

enter image description here

  • It's work perfectly sir, thanks a lot, but if I use URL directly can that work? or redirect me ? – sayou May 8 at 10:17
  • I am sorry could not understand your question. – Pritam Info 24 May 8 at 11:36
  • Don't worry sir, I'm a beginner at English, my question is: After hiding the product type and I use URL directly, ex: catalog/product/new/set/4/type/configurable that I'm going to get the page or I redirect to catalog product page? – sayou May 8 at 11:46
1

To disable access from direct URL.

You can override the class \Magento\Catalog\Block\Adminhtml\Product using di.xml preference as described below.

Assume you are using a custom module name "Company_MyModule"

step 1: create di.xml under YOUR-MAGENTO-ROOT/app/code/Company/MyModule/etc/adminhtml

File: YOUR-MAGENTO-ROOT/app/code/Company/MyModule/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">

  <preference for="Magento\Catalog\Controller\Adminhtml\Product\NewAction" 
    type="Company\My-Module\Controller\Adminhtml\Product\NewAction" />   

</config>

step 2: Create the class NewAction.php under YOUR-MAGENTO-ROOT/Company/MyModule/Controller/Adminhtml/

File : YOUR-MAGENTO-ROOT/Company/MyModule/Controller/Adminhtml/NewAction.php

<?php

namespace Company\MyModule\Controller\Adminhtml\Product;

class NewAction extends \Magento\Catalog\Controller\Adminhtml\Product\NewAction
{

   public function execute()
    {
        if (!$this->getRequest()->getParam('set')) {
            return $this->resultForwardFactory->create()->forward('noroute');
        }

         $arrAlowedTypesIds = array('simple');
         $typeId = $this->getRequest()->getParam('type');
         if(!in_array($typeId,$arrAlowedTypesIds)) {    
            return $this->resultForwardFactory->create()->forward('noroute');
         }

         return parent::execute();        

    }

}

Step 3: Run DI compile

sudo php bin/magento setup:di:compile

Product page - Magento 2

  • I try it, but it's doesn't work, if I change the URL with other, (Ex : product/new/set/4/type/simple with product/new/set/4/type/virtual ) I get the page without any problem, It's doesn't use the new Controller – sayou May 9 at 11:17
  • Please make sure your overridden controller is working by putting a die() . If the overridden controller not called then it will not work. Also, double check your di.xml. – Pritam Info 24 May 9 at 11:29
  • I do an echo with die on the new controller, and I didn't get it ( the page work without any problem ), that I need to add something to di.xml? (because your has no information) – sayou May 9 at 11:31
  • I forget to add preference to di.xml, it's work now sir, thanks a lot for your help – sayou May 9 at 11:46
  • 1
    Great!. You are most welcome. Please don't call sir. – Pritam Info 24 May 9 at 12:01

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.