Extending Magento Checkout: Adding New Fields for Order View
As a beginner in Magento development, you’re right to feel that extending checkout functionality can be daunting. But don’t worry - it’s absolutely possible with the right approach! In this post, I’ll guide you step-by-step on how to add a new input field in the checkout process that gets saved and displayed in the backend order view.
Understanding Magento Checkout Flow
Before we dive into code, let’s quickly understand the Magento checkout flow. When a customer proceeds through checkout, their data is first validated and then saved as an order object. This object contains all relevant information about the transaction, including any custom fields you might add.
The key point to remember is that changes made at the frontend must be reflected in the backend order view for administrative purposes. Let’s see how we can achieve this.
Adding a New Checkout Field
Step 1: Create the Database Column
First, you need to create the necessary database column where your field data will be stored. For example, if you want to add a “Customer Note” field:
ALTER TABLE `sales_order` ADD COLUMN `customer_note` TEXT NULL DEFAULT NULL;
This SQL command adds a new text field called customer_note (or any name you prefer) to the sales_order table.
Step 2: Modify Checkout Form
Next, you need to modify the checkout form to include your new input field. This involves editing template files and adding your field to the form:
- Navigate to
app/design/frontend/<your-theme>/Magento_Checkout/templates/form.phtml. - Look for the
<form>element. - Add your custom field within the
<fieldset class="checkout-fieldset">section, like so:
<fieldset class="checkout-fieldset">
<!-- Other fields here -->
<div class="field required">
<label for="customer_note" data-bind="attr:{title: $t('Customer Note')}">
Customer Note:
<span class="required">*</span>
</label>
<textarea name="customer_note" id="customer_note"
data-bind="html: customerNote, valueUpdate: 'keyup', visible: true"
class="input-text"></textarea>
</div>
</fieldset>
Step 3: Update the Checkout Javascript
Now you need to update the checkout JavaScript (in app/code/Magento/Checkout/view/frontend/web/js/view/form.js) to include your new field:
- Find the
initialize()function. - Add a line to assign the
customerNotevalue:
data('Magento_Checkout/js/model/payment-service').setPlaceholders({
...
customerNote: ''
});
Step 4: Save the Field to the Order
The final step is to save the data entered by the customer into the order object. This happens when the order is created.
- Navigate to
app/code/Magento/Checkout/etc/frontend/di.xml. - Add an XML node for your plugin:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Checkout\Model\Session">
<plugin name="save_customer_note"
type="<Vendor>\<Module>\Plugin\SaveOrderCustomerNote"/>
</type>
</config>
- Create your plugin class (
app/code/<Vendor>/<Module>/Plugin/SaveOrderCustomerNote.php) and implement theafterPlaceOrder()method:
public function afterPlaceOrder(\Magento\Sales\Model\Service\Order $order, \Closure $next)
{
$quote = $this->checkoutSession->getQuote();
if ($customerNote = $quote->getData('customer_note')) {
$order->setCustomerNote($customerNote);
}
return $next();
}
Conclusion
And that’s it! With these steps, you’ve successfully added a new input field to the Magento checkout process. The customer’s note will be saved in the order object and displayed in the backend order view.
Remember, the key is to understand where each part of the process fits in - from creating the database column to adding the field to the form and finally saving it with the order data. This approach provides a solid foundation for extending Magento checkout functionality.
For more advanced customizations like integrating third-party services or creating complex business logic, I recommend checking out our guide on Building Custom Magento Extensions at Laravel Company. It goes deeper into the architecture and best practices for Magento development.
Happy coding!