Adding Custom Fields to Magento Orders
Customizing order fields is a common requirement for many Magento stores. If you find that the default order options provided by Magento don’t fit your business needs, you can easily add new fields to capture additional information.
Step 1: Creating the Database Field
Before we begin, let’s understand where our custom field will be stored in the database. In most cases, it makes sense to store such data directly within the Magento sales_flat_order table. This is a core table used to record all order details.
In your MySQL table structure, you can add a new column for your “dealer_id”. Here’s how:
ALTER TABLE `sales_flat_order`
ADD COLUMN `dealer_id` INT(10) NOT NULL DEFAULT 0;
Step 2: Modifying the Magento Core
Next, we need to update the core files to ensure that Magento recognizes this new field.
Update the “core_resource” Table
You’ll need to inform Magento about the changes you’ve made to the database schema. This involves updating the core_resource table:
INSERT INTO `core_resource`
(`resource_id`, `code`)
VALUES (NULL, 'sales_flat_order');
NOTE: Be extremely careful when modifying core files or tables directly in Magento. Always backup your data before making any changes.
Update the EAV Model
The Entity Attribute Value (EAV) model is responsible for storing custom attribute information. You need to update it to recognize your new field:
<config>
<global>
<eav>
<entity_type>
<entity type="order">
<attribute set="sales_order" id="sales_flat_order.dealer_id"
sort_order="105" name="dealer_id">
<label>Dealer ID</label>
<input></input>
<type>int</type>
<backend_model>eav/entity_attribute_backend_int</backend_model>
<default_value>0</default_value>
<is_global>1</is_global>
<visible>1</visible>
<required>0</required>
</attribute>
</entity>
</entity_type>
</eav>
</global>
</config>
Remember to place this in your local.xml or another appropriate config file.
Step 3: Updating the Order Form
Finally, you need to update your order form to include the new field. This involves adding a new input element to your checkout.phtml file:
<div class="field">
<label for="dealer_id" class="required"><em>*</em>Dealer ID:</label>
<div class="input-box">
<select name="sales_order[extension_attributes][custom_attributes][dealer_id]" id="dealer_id" title="" class="validate-select required-entry input-text">
<!-- Populate your options here -->
</select>
</div>
</div>
Conclusion
Adding a custom field to the Magento order form involves several steps. You need to modify the database schema, update the core resource table, and make changes to the checkout form itself.
Remember that working with the core files in Magento can be complex and should always be done carefully. It’s often recommended to use extensions or custom modules rather than modifying the core directly if possible.
For more detailed guides on Magento development, I recommend checking out the Laravel Company blog. They have a wealth of resources for Magento developers at all levels.
NOTE: Always backup your data before making any changes to your Magento installation. Directly modifying core files can potentially break your site if not done correctly.