Magento 2 - hide & disable add type product (Simple product … etc)
-
28-02-2021 - |
Pregunta
On Magento 2, and product page, there's a list of product type,
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 )
Solución
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
Otros consejos
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