How do i get enable /disable value from Magento admin?
-
09-10-2020 - |
Question
I have created a module, where admin can enable/disable and also select any value from select options.
Now I want this data to check that values, and according to that I want to give some conditions to display menu on header. (If admin enables this, the menu will be displayed in frontend).
How do i achieve this?
Please let me know any way to do this.
Solution
Put below code in your system file
<?xml version="1.0"?>
<config>
<tabs>
<tab_name translate="label" module="Module_name">
<label>Main tab name</label>
<sort_order>500</sort_order>
</tab_name>
</tabs>
<sections>
<tab_name translate="label" module="Module_name">
<label>Sub tab name</label>
<tab>tab_name</tab>
<frontend_type>text</frontend_type>
<sort_order>110</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<general translate="label">
<label>General</label>
<frontend_type>text</frontend_type>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<enabled translate="label">
<label>Enabled:</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</enabled>
</fields>
</general>
</groups>
</tab_name>
</sections>
After that check Enable/Disable in your phtml file
<?php
$config = Mage::getStoreConfig('tab_name/general/enabled');
if($config){
echo "Enable from admin";
}else{
echo "Disable from admin";
}
OTHER TIPS
PHP/phtml files
You can use this to check your enabled/disabled settings:
Mage::getStoreConfigFlag('path/to/enable_disable') // returns true or false
For all other config values you can use:
Mage::getStoreConfig('path/to/configfield') // returns config value
Layout files
In layout.xml files you can check boolean config values like this:
<reference name="head">
<action method="append" ifconfig="path/to/enable_disable">
<block>my_block</block>
</action>
</reference>
If you want to use show something based on you config string values, you may need an helper method:
<reference name="head">
<action method="append">
<block_id helper="yourmodule/yourhelper/checkConfig"/>
</action>
</reference>
Your_Module_Helper_Data.php
public function checkConfig()
{
$config = Mage::getStoreConfig('path/to/configfield');
// your logic here
}
First of all call the value of enable disable fields from system.xml
$show = Mage::getStoreConfig('tab/general/enable_disable');
Then put your menu in if condition to display the header menu as below:
if($show == 1){
echo 'Show Menu';
}else{
echo 'Hide Menu';
}