Add extra field to magento checkout
The checkout process is one of the most crucial aspects of any eCommerce store. In Magento, customizing the checkout experience can significantly enhance user satisfaction and boost conversions. In this blog, we’ll explore practical techniques to customize the Magento checkout process using code, focusing on adding custom fields, modifying layout, and validating inputs.
1. Adding Custom Fields to Checkout
Adding custom fields to the checkout page can help gather additional information from users. Magento allows you to extend the checkout form easily.
Code Example: Adding a Custom Text Field
// app/code/Webstair/CustomCheckout/Plugin/Checkout/LayoutProcessor.php
namespace Webstair\CustomCheckout\Plugin\Checkout;
class LayoutProcessor
{
public function afterProcess(
\Magento\Checkout\Block\Checkout\LayoutProcessor $subject,
array $jsLayout
) {
$customField = [
'component' => 'Magento_Ui/js/form/element/text',
'config' => ['customScope' => 'shippingAddress', 'template' => 'ui/form/field'],
'dataScope' => 'shippingAddress.custom_field',
'label' => __('Custom Field'),
'validation' => ['required-entry' => true],
'sortOrder' => 250,
];
$jsLayout['components']['checkout']['children']['steps']['children']['shipping-step']
['children']['shippingAddress']['children']['shipping-address-fieldset']['children']['custom_field'] = $customField;
return $jsLayout;
}
}
This code adds a custom text field to the shipping address form in the checkout.
2. Validating Custom Fields
To ensure data accuracy, it’s essential to validate custom fields before submission.
Code Example: Adding Validation
'validation' => ['required-entry' => true, 'min_text_length' => 3],
This code snippet ensures that the field is not empty and has a minimum length of 3 characters.
3. Customizing Checkout Layout
Changing the layout of the checkout page can improve the overall shopping experience. You can modify the layout through XML files.
Code Example: Adjusting Layout
<!-- app/design/frontend/YourTheme/checkout_index_index.xml -->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="checkout.root">
<block class="Magento\Checkout\Block\Onepage" name="custom.checkout.block" template="Webstair_CustomCheckout::custom.phtml" />
</referenceContainer>
</body>
</page>
This code inserts a custom block into the checkout layout.
4. Adding Custom JavaScript for Dynamic Fields
To make the checkout more interactive, you might want to add dynamic field handling.
Code Example:
require(['jquery', 'domReady!'], function ($) {
$('#custom_field').on('change', function () {
alert('Custom field changed: ' + $(this).val());
});
});
This script triggers an alert whenever the custom field value changes.
5. Handling Custom Data on the Server Side
Processing the custom data requires adding it to the order object.
Code Example: Save Custom Field Data
// Observer to save custom data
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
$customFieldValue = $observer->getEvent()->getRequest()->getParam('custom_field');
$order->setData('custom_field', $customFieldValue);
$order->save();
}
This observer saves the custom field data in the order entity.
Conclusion
Customizing the Magento checkout process can greatly enhance user experience and streamline the purchasing journey. By adding custom fields, validating user input, and adjusting the layout, you can make the checkout more user-friendly and efficient. Implement these techniques in your store and witness improved conversion rates and customer satisfaction.
Need help with checkout customization? Get in touch with us at Webstair Technology to enhance your Magento store today!