I need to add custom text field to "add review" form, for example email.
There are already some threads on web where people are asking the same thing, but none of them has a solid answer. Most suggest to modify core files directly, or run manual SQL queries.
If you know some open-source plugin that adds such feature, or you already implemented this somewhere, please share how you did it.
Thank you!
Best Answer
You first need to answer the question what do you want to do with the new data. Should it be stored with the order? Should it be sent somewhere by email?
For now, I'll just assume you want to store it with the order and display it in the backend with the order details.
You will have to take care of the following things:
Lets go through one by one.
I'm assuming you know how to create a basic module. In the example code I'll assume the module
Namespace_ReviewField
.1. Add one attribute for each input field to the order
To add the order attribute, create a setup script with the following contents, after adding a setup resource and a version number to your module:
2. Display the input field(s) on the review step
To display the input field in the review step, first add a layout XML file to your module:
Then create the layout XML file base/default/layout/namespace/reviewfield.xml with the following contents:
Finally you create the template base/default/template/namespace/reviewfield/form.phtml
3. Make sure the input fields are submitted when the order is saved
Now the field will be displayed after the totals on the review step. But because the field is not inside of any form, you need to take care it is submitted when the order is saved.
Add the following JavaScript to your form.phtml template.
4. Set the submitted values on the order
Now the field value will be sent to Magento when the order is submitted. Only so far, nothing else happens with it. You have to set the submitted value on the order. A good way to do that is using an event observer for the
checkout_type_onepage_save_order
event.The observer class itself is very simple:
And that takes care of the frontend side of things.
5. Add a new tab to the order info page in the backend
Unfortunately, there is no "nice" way to add content to the general order info page. It's much more compatible to add a new tab with the new content you want to display. To do that, you first need to add an adminhtml layout XML file:
The new tab itself is then added in the layout XML declaration. There are several ways this can be done, but I suggest you use the probably most straightforward way by adding the following code to the file adminhtml/default/layout/namespace/reviewfield.xml
6. Display the field values
Now you only need to create the block that you specified in the layout XML and a matching template. The block class has to implement
Mage_Adminhtml_Block_Widget_Tab_Interface
, otherwise a "Wrong Tab Configuration" exception will be thrown.Here is a simple example:
The final missing thing is the template adminhtml/default/template/namespace/order/tab/reviewfield.phtml. Just make sure you properly escape any user input to avoid XSS holes.
Hope that helps you along the way to implement the functionality you require.