2

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/

enter image description here

3

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!

| improve this answer | |
  • 1
    its works what i m expect thank you :) – divya sekar Nov 27 '19 at 13:11
  • You're welcome :) @divyasekar – Kishan Savaliya Nov 27 '19 at 13:13
  • can you please tell me how ro remove cart attributes option from condition field prnt.sc/qabr5i – divya sekar Dec 13 '19 at 12:10
  • 1
    try to use unset($conditions[4]); before return. – Kishan Savaliya Dec 13 '19 at 12:51
  • 1
    thanks sir, its working fine – divya sekar Dec 13 '19 at 13:09

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.