How to hide some option in condition field in admin?
Domanda
How to hide some option
in condition field
in admin
?
I used this blog to create a condition action:
https://www.mageworx.com/blog/2016/09/magento-2-module-with-conditions-model-fieldset/
Soluzione
You need to override this file
vendor/magento/module-sales-rule/Model/Rule/Condition/Combine.php
in your custom module here
app/code/Vendor/Rules/Model/Rule/Condition/Combine.php
Content for this file is..
<?php
namespace Vendor\Rules\Model\Rule\Condition;
class Combine extends \Magento\SalesRule\Model\Rule\Condition\Combine
{
protected $_eventManager = null;
protected $_conditionAddress;
protected $request;
public function __construct(
\Magento\Rule\Model\Condition\Context $context,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\SalesRule\Model\Rule\Condition\Address $conditionAddress,
\Magento\Framework\App\Request\Http $request,
array $data = []
) {
$this->request = $request;
parent::__construct($context, $eventManager, $conditionAddress, $data);
$this->setType(\Magento\SalesRule\Model\Rule\Condition\Combine::class);
}
public function getNewChildSelectOptions()
{
$conditions = parent::getNewChildSelectOptions();
$routerName = $this->request->getRouteName(); //'vendor_rules';
if($routerName == 'vendor_rules'){ //Here you can add your router name in condition.
$removeProductFound = array_search('Magento\SalesRule\Model\Rule\Condition\Product\Found', array_column($conditions, 'value'));
unset($conditions[$removeProductFound]);
$removeSubselect = array_search('Magento\SalesRule\Model\Rule\Condition\Combine', array_column($conditions, 'value'));
unset($conditions[$removeSubselect]);
}
return $conditions;
}
}
Here I've added one condition based on router name you can replace your router name there, and I've unset that both options from that array. So now you will not get that both options only in your custom rule module.
You need to add this line in your di.xml
file here
app/code/Vendor/Rules/etc/di.xml
Content for this file is..
<?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\SalesRule\Model\Rule\Condition\Combine" type="Vendor\Rules\Model\Rule\Condition\Combine" />
</config>
Run following command once..
php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento cache:clean
php bin/magento cache:flush
Hope this will help you!