How to add dynamic radio buttons in custom checkout step?

How to add dynamic radio buttons in custom checkout step?

September 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add dynamic radio buttons in custom checkout step?

Adding Dynamic Radio Buttons in Custom Checkout Step

As a senior developer, I understand your challenge with adding dynamic radio buttons for a custom pickup step in Magento’s checkout process. You want to display the options based on the customer’s selected postal code and use Knockout.js to manage the data. Let me guide you through this process.

Understanding the Current Setup

Your current layout processor (LayoutProcessorPlugin.php) retrieves nearby stores based on the user’s postcode and populates them as radio button options using Magento UI’s checkbox-set component. This approach is efficient for processing the initial checkout step but lacks flexibility in terms of data transfer between the HTML template and Knockout.js.

The Problem at Hand

The issue arises because you’re populating the radio buttons via a layout processor, which is visible across all steps. To make it specific to your custom pickup step, you need to handle the data manipulation within that step’s JavaScript file (pickup-store-step.js).

A Step-by-Step Solution

  1. Understand the Data Flow

    The data flow should work as follows:

    • Retrieve stores based on the user’s postcode in LayoutProcessorPlugin.php.
    • Pass these options to your custom pickup-store-step component.
    • Use Knockout.js in pickup-store-step.js to bind these options to the radio buttons.
  2. Modify Layout Processor Plugin

    In your LayoutProcessorPlugin.php, update the return statement to include the retrieved store options directly under the checkout component:

    $jsLayout['components']['checkout']['children']['steps']['children']['pickup-store-step']['children']['pickup_stores'] = [
        'component' => 'Namespace_Module/js/view/pickup-stores',
        'config' => [
            'template' => 'Namespace_Module/pickup-stores',
            'options' => $additionalOptions,
            'multiple' => false,
            'id' => 'pickup_stores'
        ],
        // Other configuration as needed
    ];
    
  3. Create a New Component for Radio Buttons

    Create a new JavaScript file named pickup-stores.js to handle the radio buttons:

    define([
        'jquery',
        'underscore',
        'uiComponent'
    ], function ($, _, Component) {
        'use strict';
    
        return Component.extend({
            defaults: {
                template: 'Namespace_Module/pickup-stores'
            },
            // Add your logic here to display the radio buttons
            options: [],
    
            initialize: function () {
                this._super();
                this.options = window.checkoutConfig.pickup_stores;
            }
        });
    });
    
  4. Update pickup-store-step.xml

    Update the checkout_index_index.xml file to include your new pickup-stores component:

    <item name="pickup_stores" xsi:type="array">
        <item name="component" xsi:type="string">Namespace_Module/js/view/pickup-stores</item>
        <item name="sortOrder" xsi:type="string">300</item>
    </item>
    
  5. Update pickup-store-step.js

    In your pickup-store-step.js, add the necessary Knockout.js bindings to your HTML template:

    // Add this code inside the initialize function of pickup-store-step.js
    ko.bindingHandlers.radioButtons = {
        init: function (element, valueAccessor) {
            var options = valueAccessor();
            $(element).html(options.map(function (option) {
                return `<div><input type="radio" name="pickup_store" id="${option.value}" value="${option.value}"><label for="${option.value}">${option.label}</label></div>`;
            }));
    
            // Add click event to handle radio button selection
            $(element).find('input[type="radio"]').on('change', function () {
                var selectedStore = $(this).val();
                // Send the selectedStore value back to your parent component or process it as needed
                console.log(selectedStore);
            });
        }
    };
    
  6. Update pickup-store-step.html

    In your pickup-store-step.html, update the radio button section:

    <div data-bind="radioButtons: options"></div>
    
  7. Compile and Merge JS Files

    Make sure to compile and merge the JavaScript files using Magento’s built-in tools or third-party libraries like RequireJS.

Conclusion

Adding dynamic radio buttons to a custom checkout step involves careful management of data flow between layout processors, JavaScript components, and HTML templates. By following this approach, you can create a robust solution that handles data retrieval based on the user’s postcode and displays the options specifically in your pickup step using Knockout.js.

Remember to replace Namespace_Module with your actual module name throughout these examples. This will ensure that the radio buttons are displayed correctly during checkout, providing a seamless experience for your customers.

For more detailed tutorials and best practices on Magento 2 development, I recommend exploring Laravel Company’s blog. They offer in-depth guides and practical tips from industry experts.

Checkout Extensions Themes Customers

Our network