I need to display message to admin in front of some custom product attribute, For example "Donation" fields which take decimals, I need to display message to admin that say's 20% is a minimum donation for this product.

This 20% will be different for other product, for some product it may be 15% or 10%. I have custom module that stores donation percentages to database.

My question is from database how Can I display message [like field comment] with dynamic message content.


You cannot set it from the database.
You will have to code for it.
You can make use of the after_element_html feature that the form builder offers.
So declaring a field like this:

$fieldset->addField('identifier_here', 'type_here',
        'name'      => 'name_here',
        'label'     => 'Label here',
        'required'  => true|false,
        'after_element_html'      => 'Some text after the element',

will make your text Some text after the element appear after the element. It can be anything, even some html or javascript.
But there is a catch. The product add/edit form is not built using code as shown above. You cannot control each field. The fields are generated based on the attributes.
But you have the event adminhtml_catalog_product_edit_prepare_form that is dispatched for every tab that contains product attributes.
You can try to observe it like this (untested code):

public function prepareEditForm($observer) 
    $form = $observer->getEvent()->getForm();
    //let's say you want to add some message for the attribute with code `donation`
    $donationElement = $form->getElement('donation');
    if ($donationElement) {
         $message = ...determine the string to display....
    return $this;

There is also an other approach. Each attribute supports a custom form renderer. This feature basically allows you to have the input be rendered using your own class and your own phtml file. This seams like a more complicated approach, but if you want to give it a try check this answer. It's for a different problem, but the approach is the same.

  • Which event I need to use in this case? – Charlie Jun 10 '15 at 12:11
  • I have created my custom product attribute by admin backend option Manage Attribute, So may get after_element_html attribute from there? – Charlie Jun 10 '15 at 12:13
  • As I explained, you cannot get the after_element_html from the db (unless you add a column to the table and implement that logic, but it won't be dynamic). You can use the adminhtml_catalog_product_edit_prepare_form event as I explained. – Marius Jun 10 '15 at 12:30
  • Your example worked exactly for my question, But I have set note attribute instead of setAfterElementHtml. $donationElement->setNote($message); Thank You Marius. – Charlie Jun 11 '15 at 11:45

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.