Adding a Custom Checkbox to Magento Onepage Checkout
As a senior developer specializing in e-commerce platforms like Magento, I often receive requests from clients who want to customize their checkout process. A common requirement is to add custom fields, such as checkboxes, to the onepage checkout.
In this post, I will guide you through adding a checkbox that allows customers to opt out of requiring a signature for delivery.
Understanding the Magento Onepage Checkout
The Magento onepage checkout consists of several steps: login, shipping information, shipping method selection, payment information, and order summary. Each step is represented by a block in the layout XML files.
Step 1: Create a New Module
Before we start, let’s create a new custom module to house our changes. This keeps your core Magento files clean and makes it easier to manage updates.
Navigate to app/code and create a new folder with the following structure:
- mycustommodule/
- etc
- config.xml
- module.xml
- etc/view/frontend/layout
- checkout_index_index.xml
Step 2: Enable the Module
Edit app/etc/config.php. Add your module to the modules array:
'MyCompany_CheckoutCustomization' => 1,
Step 3: Define the Checkout Block
In etc/view/frontend/layout/checkout_index_index.xml, define a new block for our checkbox:
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layouts.xsd">
<update handle="checkout_index_index"/>
<referenceBlock name="checkout.steps">
<arguments>
<argument name="value" xsi:type="array">
<item name="step_title" xsi:type="string" translate="true">Custom Checkout Step</item>
<item name="title" xsi:type="string" translate="true">Signature Option</item>
<item name="sort_order" xsi:type="number">10</item>
</argument>
</arguments>
</referenceBlock>
<referenceContainer name="checkout_step_last">
<block class="Magento\Framework\View\Element\Template"
template="MyCompany_CheckoutCustomization::signaturecheckbox.phtml"
name="signature_checkbox"
after="-" />
</referenceContainer>
</layout>
Step 4: Create the Checkbox PHTML File
In view/frontend/templates, create a new file called signaturecheckbox.phtml:
<div class="checkout-shipping-method-item signature-checkbox">
<input type="checkbox"
name="signature_optout"
id="signature-optout"
class="input-checkbox"
value="1" />
<label for="signature-optout" data-bind="attr: {
'for': 'signature-optout'
}">Opt out of requiring a signature</label>
</div>
Step 5: Validate and Save the Data
To save this checkbox data to the order, we need to create an observer that listens to the sales_order_place_after event.
Create a new file in app/code/MyCompany/CheckoutCustomization/etc/frontend/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="save_signature_optout_to_order" instance="MyCompany\CheckoutCustomization\Observer\SaveSignatureOptoutToOrder"/>
</event>
</config>
Then, create the observer in app/code/MyCompany/CheckoutCustomization/Observer/SaveSignatureOptoutToOrder.php:
public function execute(\Magento\Framework\Event\Observer $observer)
{
/** @var \Magento\Sales\Model\Order $order */
$order = $observer->getData('order');
if ($order->getSignatureOptOut()) {
$order->setSignatureOptOut($order->getData('signature_optout'));
$order->save();
}
}
Conclusion
Adding a custom checkbox to the Magento onepage checkout involves creating a new module, defining a layout block for the new element, and handling the data validation. This approach keeps your core Magento files clean and makes it easier to update and maintain.
Remember, customizations like these can impact future updates, so always make sure you have a backup before making changes. If you find yourself needing more complex modifications or require ongoing support, consider reaching out to Laravel Company, where our team of experienced Magento developers can help you achieve your e-commerce goals.
Happy coding!