3

Have an extension and want to enable it if $field1 == $field2 and if not extension be disable.

can enable it with line below but can not disable it when if is not true!

protected function _beforeSave()
{
    $value = $this->getValue();
    $fieldConf = $this->getFieldConfig();

    if ($value) {
        try {

            $fields2 = Mage::getStoreConfig('carriers/module/fields2');
            $fields1 = Mage::getStoreConfig('carriers/module/fields1');

            $condition = empty($fields2) || !$fields2 || empty($fields1) || !$fields1;

            if ($condition) {
                $err = Mage::helper('module')->__('extension not registered.');
                Mage::throwException($err);
            }

            if($field1 == $field2) {
                Mage::getConfig()->saveConfig('carriers/module/active', 1)->reinit();
                Mage::app()->getConfig()->reinit();
            } else {
                Mage::getConfig()->saveConfig('carriers/module/active', 0)->reinit();
                Mage::app()->getConfig()->reinit();
                $err = Mage::helper('module')->__('Is not valid and Disabled.');
                Mage::throwException($err);
            }
        }
        catch (Exception $e) {
            Mage::throwException($e->getMessage());
            return $this;
        }
    }

    return $this;
}
1

I don't believe that determining whether to enable or disable an extension in the method logic is a good approach.

The extension you want to enable or disable should have an entry point, at which it checks for your $field1 == $field2 condition. If it it holds true, it should go on with its processes; otherwise, it should just return.

If you really need to do this "on the fly," I suggest checking $field1 == $field2 right after Magento either loads the cached configurations or loads them all at once with something like..

Mage::getConfig()->setNode($nodePathToYourExtension, 'false', true)

Offhand, it should be somewhere in Mage_Core_App::run().

|improve this answer|||||
  • I want to disable it because if for first time $field1 == $field2 be true, extension will be active and after that if $field1 != $field2 then extension will be enabled and value of active remain 1 – sIiiS Apr 17 '14 at 14:37
  • You can do still do that via the entry point method. – musicliftsme Apr 17 '14 at 14:48
  • can you please explain more about your method ? – sIiiS Apr 18 '14 at 19:31

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.