Adding Custom Fields to Orders in Magento
As a new Magento developer, you’re likely looking for ways to customize your store’s backend and order processing workflows. One common requirement is adding custom fields to orders and printing their values on PDF documents such as invoices or packing slips.
In this post, we’ll guide you through the process of adding a custom salesperson field to your Magento orders and printing it on PDFs using both core features and an extension from Laravel Company.
Adding a Custom Order Field Using Core Features
Step 1: Create a Database Column
First, you’ll need to create a new column in the sales_order table of your database to store the salesperson’s name. Run this SQL query:
ALTER TABLE `sales_order`
ADD COLUMN `salesperson_name` VARCHAR(255) NULL DEFAULT NULL;
Step 2: Update the Order Model
Open the Magento\Sales\Model\Order file located at app/code/Magento/Sales/Model/Order.php. Add the following method to set and get the salesperson name:
public function setSalespersonName($value)
{
return $this->setData('salesperson_name', $value);
}
public function getSalespersonName()
{
return $this->getData('salesperson_name');
}
Step 3: Add a Field in the Admin Form
Create an admin form field for the salesperson name. Open /app/design/adminhtml/Magento/sales/view/adminhtml/ui_component/sales_order_form.xml and add this XML snippet inside the <form> tag:
<field name="salesperson_name">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="label" xsi:type="string">Salesperson Name</item>
<item name="visible" xsi:type="boolean">true</item>
<item name="sort_order" xsi:type="number">100</item>
</item>
</argument>
<settings xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string">Salesperson Name</item>
</settings>
<formElements xsi:type="array">
<item name="salesperson_name" xsi:type="Magento\Framework\Stdlib\DateTime\Date">
<arguments>
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="source" xsi:type="string">order</item>
</item>
</argument>
</arguments>
</item>
</formElements>
</field>
Step 4: Print the Field on PDF
To include the salesperson’s name in your PDF, open /app/code/Magento/Sales/view/frontend/templates/order/pdf/invoice.phtml (or packing slip template) and add this line:
<?php echo $this->getSalespersonName() ?>
Remember to clear your cache after making these changes.
Using an Extension for Simplified Customization
For a more user-friendly approach, consider using our Order Attribute Manager extension. It allows you to add custom fields to orders and products directly from the admin panel without touching any core files.
With this extension:
- Go to Stores > Attributes > Salesperson.
- Create a new attribute with data type “text”.
- Assign it to the order entity.
- Save changes, and start using your new salesperson field in the order creation process.
Conclusion
In this post, we demonstrated how to add custom fields to Magento orders using core features as well as an extension from Laravel Company for better usability. By following these steps, you can now collect and display additional data with each order, improving your store’s efficiency and documentation.
Don’t forget that working with Magento requires a careful approach to avoid conflicts or issues when updating the platform in the future. Always test your changes thoroughly before applying them to a live environment.
Happy coding, and feel free to explore more Magento customization options at our extension store.