1

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.

3

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.

| improve this answer | |
4

Check out app\code\core\Mage\Adminhtml\Block\Tax\Rate\Form.php for an example of how Magento does this, which is slightly different than Bijal Bhavsar's method.

First it defines it's Yes/No select

$fieldset->addField('zip_is_range', 'select', array(
    'name'    => 'zip_is_range',
    'label'   => Mage::helper('tax')->__('Zip/Post is Range'),
    'options' => array(
        '0' => Mage::helper('tax')->__('No'),
        '1' => Mage::helper('tax')->__('Yes'),
    )
));

Then another field, (which will be hidden sometimes)

$fieldset->addField('zip_from', 'text', array(
    'name'      => 'zip_from',
    'label'     => Mage::helper('tax')->__('Range From'),
    'required'  => true,
    'maxlength' => 9,
    'class'     => 'validate-digits'
));

Then it sets the dependance, right above _prepareForm()

$this->setChild(
    'form_after',
    $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('zip_is_range', 'zip_is_range')
        ->addFieldMap('zip_from', 'zip_from')
        ->addFieldDependence('zip_from', 'zip_is_range', '1')
);

Note on prefixes

If you at any point set a prefix like this: $form->setHtmlIdPrefix('mymodule_'); then you'll need to put the prefix in the dependence block like so:

$this->setChild(
    'form_after',
    $this->getLayout()->createBlock('adminhtml/widget_form_element_dependence')
        ->addFieldMap('mymodule_zip_is_range', 'mymodule_zip_is_range')
        ->addFieldMap('mymodule_zip_from', 'mymodule_zip_from')
        ->addFieldDependence('mymodule_zip_from', 'mymodule_zip_is_range', '1')
);
| improve this answer | |
1

You can add js files on the admin product page using the layout xml files: in tag: you can add either a js file in head tag:

<reference name="head">
   <action method="addItem"><type>skin_js</type><name>path to js</name></action>
</reference> 

Note: this should be in the magento skin adminhtml folder.

Or you could add a js block if you also need some php logic: For example:

<reference name="js">            
   <block type="core/template" name="custom.js" template="my path to js phtml"/>
</reference>

Note: this file should go in the adminhtml theme

| improve this answer | |

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.