Magento – Product attribute show/hide in admin depending on other attribute value

attributes

I have a product attribute but it is only relevant if another product attribute is selected (a dropdown yes/no) value.

In the admin view I would like show/hide my custom attribute depending on the yes/no value of the other system attribute.

I believe this is possible using javascript but im not sure how to get the javascript on the page with my attribute.

Best Answer

We can use 'depends' tag in system.xml in field which we have to show/hide depends on 'yes/no' field.

Below are syntax you can use:

<depends><enabled>1</enabled></depends>

In above syntax 'enabled' is a field with 'yes/no' values.

Above code is for system >> Configuration settings .

To add any field depends on 'yes/no' in product form you can use below code:

  $productField = $fieldset->addField('enabled', 'select', array(
        'label'     => Mage::helper('adminhtml')->__('Enabled'),
        'title'     => Mage::helper('adminhtml')->__('Enabled'),
        'name'      => 'enabled',
        'options'   => array(
            0 => Mage::helper('adminhtml')->__('No'),
            1 => Mage::helper('adminhtml')->__('Yes'),  
        ),
        'onclick' => 'toggleYesNo()',
    ));
    $productField->setAfterElementHtml('
        <script>
        function toggleFixedShipping() {
           $("idToShowHide").toggle();
        }
        </script>
    ');

Here 'idToShowHide' id will be show/hide field's ID.

I hope it solves your concern.