Magento: Add Instructions to Form Field in Admin Product Add/Edit

I made some custom functionality that can happen with a product’s Short Description. Depending on what is put in that textarea, icons and special product-specific links can be displayed on that product’s view page. It requires a specific format though for it to work – and instead of emailing the client and having them forget how to do it correctly, it would make more sense to put instructions next to the Short Description field in the Admin product add/edit page.

For this example, we’ll be adding instructions to the Short Description. If you take a look at the source code, you’ll see that the short description is displayed with this code:

1
2
3
4
5
6
<tr class="hidden">
    <td class="label"><label for="short_description">Short Description <span class="required">*</span></label></td>
    <td class="value"><textarea id="short_description" name="product[short_description]" class=" required-entry required-entry textarea" type="textarea" ></textarea></td>
    <td class="scope-label"></td>
    <td><small></small></td>
</tr>

So, we know that the Short Description has a unique identifier with “short_description”. We can use this when we modify the code. The file you you will need to modify to add the instructions is: /app/design/adminhtml/default/default/template/catalog/form/renderer/fieldset/element.phtml. But, you don’t want to edit this file directly because it is a default admin theme file. If you edit that file, you always run the risk of having it overwritten when you do an update.

You’ll want to follow the same guidelines that you use to create a new frontend theme. So, copy that file to a location like this: /app/design/adminhtml/default/YOURTHEME/template/catalog/form/renderer/fieldset/element.phtml. You’ll want to make sure that YOURTHEME is the same theme that you have designated in System->Configuration->Design (If you set your theme by going to System->Design and applying a design change – this will not work).

The code in this file is looped over when Magento creates the product add/edit pages. The file is quite small and simple. Open it up and look for the following code:

1
2
3
4
<tr class="hidden">
    <td class="label"><?php echo trim($this->getElementLabelHtml()) ?></td>
    <td class="value"><?php echo trim($this->getElementHtml()) ?></td>
    <td class="scope-label"><?php echo $this->getScopeLabel() ?></td>

The <td class=”scope-label”> element is where we are going to place our instructions. We can make the instructions show up ONLY for the Short Description by plugging in some code to only display if we find “short_description” from within the $this->getElementHtml() output.

So, modify that code to look something like the following (there may be a much better way to do this – I’m no PHP guru, but this code works):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<tr class="hidden">
    <td class="label"><?php echo trim($this->getElementLabelHtml()) ?></td>
    <td class="value"><?php echo trim($this->getElementHtml()) ?></td>
 
    <?php if (strpos(trim($this->getElementHtml()), 'short_description') !== false) { ?>
        <td class="scope-label">
	    <?php echo $this->getScopeLabel() ?><br />
	    <strong>To Add a generic shop at icon and link:</strong><br />
	    Simply add 'shopnbc', 'thefind', 'bestbuy', or 'fingerhut'.  One per line<br /><br />
	    <strong>To Add a product-specific link:</strong><br />
	    Use this format: "[shopnbc: http://www.shopnbc.com/the-rest-of-the-link]"
	</td>
    <?php } else { ?>
	<td class="scope-label"><?php echo $this->getScopeLabel() ?></td>
    <?php } ?>

That’s all there is to it. When you’ve got that done, your admin product add/edit should now look like this:

Conclusion

Now you know an easy way to add field-specific instructions or notes if needed in the admin product add/edit forms. I know the PHP is a little sloppy – so if you have a better, more pragmatic way to do it, please let me know. If you have any questions – please post a comment.

This entry was posted in Magento, PHP. Bookmark the permalink.

3 Responses to Magento: Add Instructions to Form Field in Admin Product Add/Edit

  1. Prashant says:

    I have to remove compulsory field of “Shor Description”.. but i have not found any file for that. so can you help me for that.. How can i remove compulsory field with proper guidline?

  2. serks says:

    This is great….was wondering if there was a way to put instructions beside the CMS page content box or even a Static block content box. That would be really helpfull.

  3. Alma says:

    This was very helfull for me…thnx