Adding Custom Fields to Magento Checkout and Admin
Customizing the checkout process is a powerful way to enhance your Magento store’s user experience while streamlining internal processes. Let me guide you through adding custom fields to both the front-end checkout step and the admin panel’s ‘Create Order’ section.
Understanding the Checkout Process
The Magento checkout process consists of several steps, including Address Information, Shipping Method, Payment Method, and finally, Order Review. Each step can be extended with additional fields using plugins or observers.
Adding a Custom Field to the Front-End Checkout Step
To add a custom field like a PO number, job name, or customer comments to the checkout summary (Order Review) step, you’ll need to create an observer.
-
Create an Observer Module: First, generate a new module using Magento’s built-in module generator command:
php bin/magento setup:module:create --name=YourCompany_CustomCheckout -
Create the Observer Class: In your newly created module, navigate to the
etcdirectory and open theevents.xmlfile. Add the following XML code to register an observer for the ‘checkout_type_oneone_process_before’ event:<event name="checkout_type_onepage_process_before"> <observer name="yourcompany_customcheckout_observer" instance="YourCompany\CustomCheckout\Observer\AddCustomFieldToCheckout"/> </event> -
Implement the Observer: Create a new observer class
AddCustomFieldToCheckoutinObserver. In this class, use thesetDatamethod to add your custom field:public function execute(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $observer->getQuote(); // Add a new quote item with your custom field $quoteItem = $quote->getItemsCollection()->addItem( new \Magento\Quote\Model\Quote\Item([ 'item_id' => null, 'product_id' => 0, // Set to zero for a custom field 'qty' => 1, 'price' => 0, 'name' => 'Your Custom Field Name', 'sku' => 'YOUR_CUSTOM_FIELD_SKU', 'tax_class_id' => 0, 'weight' => 0 ]) ); $quoteItem->setData('additional_options', [ 'additional_field_name' => 'Value of your custom field' ]); return $this; }
Adding Custom Fields to the Admin “Create Order” Section
To add a custom field to the admin panel’s ‘Create Order’ section, you can use layout XML files.
-
Override the Adminhtml Layout: In your module’s
etcdirectory, create a new file calledadminhtml.xml. Add the following code:<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layouts.xsd"> <layout id="sales_order_create_index_index"> <block type="core/text_list" name="additional-fields" as="additional-fields"><![CDATA[ <!-- Add your custom field here --> <label for="your_custom_field_id">Your Custom Field Name:</label> <input type="text" class="admin__field-control" id="your_custom_field_id" name="your_custom_field_id"/> ]]></block> </layout> </layout> -
Save and Sync: Save the file and sync your Magento theme with the command:
php bin/magento setup:static-content:deploy
Remember, adding custom fields to the checkout process requires careful consideration of user experience and server load. It’s essential to keep these fields relevant and minimal to ensure they don’t negatively impact the checkout flow.
Conclusion
Adding custom fields to Magento’s checkout and admin panel involves a combination of XML layout files and PHP observers. By following this guide, you can now enhance your store’s checkout process with custom fields like PO numbers, job names, or customer comments, making it easier for customers to provide necessary information while streamlining your order management.
For more advanced customizations and integration tips, I recommend exploring the Laravel Company blog for in-depth tutorials on Magento development best practices.