How to add custom field in admin order create page in Magento2

In this post, we explain how to add custom field in admin order create page with enable/disable system configuration. In Magento2, you can figure out lot of frontend customizations examples but only limited examples available in the internet for admin customizations. Subscribe to Bnethos for all the Magento2 problems and customizations

Steps to Add Custom Field in Admin Order Create Page in Magento2

  1. Create registration.php
  2. Create module.xml
  3. Register a module
  4. Create a System Configuration
  5. Create a column in table
  6. Create a new custom field
  7. Save the custom field in the table.

Step 1: Create registration.php

In this step, we will add registration.php under app/code/Bnethos/AdminOrderCustomField/registration.php

Step 2: Create module.xml

In this step, we will add module.xml under app/code/Bnethos/AdminOrderCustomField/etc/module.xml

Step 3: Register a Module

In this step, we will register the module using php bin/magento setup:upgrade and after registered the module make sure the module is enabled using php bin/magento module:status Bnethos_AdminOrderCustomField

Step 4: Create a System Configuration

In this step, we will create a system configuration
Create a system.xml under app/code/Bnethos/AdminOrderCustomField/etc/adminhtml/system.xml. system.xml is used to create a configuration in magento admin.

Create a config.xml under app/code/Bnethos/AdminOrderCustomField/etc/config.xml . config.xml is used to set default value for the above created system configuration field.

The result of the system configuration files in the below image. Using this field you can disable/ enable the custom field in admin order create.

Configuration

Step 5: Create a column in table

In this step, we will create a new column in the sales_order an quote table using db_schema.xml under app/code/Bnethos/AdminOrderCustomField/etc/db_schema.xml

Make sure to generate db_schema_whitelist.json using php bin/magento setup:db-declaration:generate-whitelist –module-name=Bnethos_AdminOrderCustomField it will generate a file under app/code/Bnethos/AdminOrderCustomField/etc/db_schema_whitelist.json

Step 6: Create a new custom field

In this step, We will create a new custom field to display in admin order create page.

Create purchase_order_number.phtml under app/code/Bnethos/AdminOrderCustomField/view/adminhtml/templates/order/purchase_order_number.phtml . This code will create new admin text field.

Create a Plugin to display the newly created text field in admin order create. Register a plugin in di.xml under app/code/Bnethos/AdminOrderCustomField/etc/di.xml

Create a new plugin OrderCreatePlugin.php under app/code/Bnethos/AdminOrderCustomField/Plugin/Order/OrderCreatePlugin.php

The above two files will create a new text field in admin order create in the below image.

Result

Step 7: Save the custom field in the table.

Now we need to write a logic to save the input value of the newly create custom file.

Create a Plugin for Magento\Sales\Model\AdminOrder\Create to save the input value in quote. Register a plugin in di.xml under app/code/Bnethos/AdminOrderCustomField/etc/di.xml

Create a plugin PurchaseOrderNumberSetQuote.php under app/code/Bnethos/AdminOrderCustomField/Plugin/Order/PurchaseOrderNumberSetQuote.php. This plugin will add the input data to quote table where we added our new column purchase_order_number

Now we need to write a logic to save the quote data to order. We ll use sales_model_service_quote_submit_before event to save quote data to order. Create events.xml under app/code/Bnethos/AdminOrderCustomField/etc/events.xml

Create a new Observer PurchaseOrderNumberSaver.php file under app/code/Bnethos/AdminOrderCustomField/Observer/Order/PurchaseOrderNumberSaver.php

That’s it! The custom input value will save in the table sales_order under purchase_order_number column after we created a new order from Admin.

If you need more help regarding this post, feel free to ask in the Comments section below.

I would be happy to solve your queries.

Related Topics : How to display custom order field in order, invoice and shipment page in Magento2 admin

3 thoughts on “How to add custom field in admin order create page in Magento2”

Leave a Comment