Out of the box, Magento checkout consists of two steps:
Shipping Information
Review and Payment Information
On the Shipping Information checkout step Magento renders all addresses previously saved by a shopper. The shopper can then select the one to be used for shipping by clicking it. The default address renderers cover the majority of use cases, but Magento provides way to register custom address renderer for a new address type.
This topic describes how to implement a custom shipping address renderer.
To implement shipping address rendering in checkout, you need to take the following steps:
Create the JS renderer component (shipping address renderer)
Your shipping address renderer must be implemented as a JavaScriptUI component. That is, it must be a RequireJS module, and must return a factory function, that takes a configurable object.
For the sake of compatibility, upgradability and easy maintenance, do not edit the default Magento code. Instead add your customizations in a separate module. For your checkout customization to be applied correctly, your custom module must depend on the Magento_Checkout module. Module dependencies are specified in the module’s composer.json. Do not use Ui for your custom module name, because %Vendor%_Ui notation, required when specifying paths, might cause issues.
In your custom module directory, create the component’s .js file (shipping address renderer). It must be located under the <your_module_dir>/view/frontend/web/js/view/ directory.
The general view of the shipping address renderer is the following:
Create a template for the shipping address renderer
In your custom module directory, create a new <your_module_dir>/view/frontend/web/template/<your_template>.html file. The template can use Knockout JS syntax.
The template should contain a button for setting the address to be used for shipping.
Create the JS model for the shipping rate processor
A shipping rate processor is responsible for retrieving the shipping rates available for the given shipping address.
In your custom module directory, create the component’s .js file for the processor. It must be located under the <your_module_dir>/view/frontend/web/js/model/ directory.
Here you need to specify the URL used for calculating the shipping rates for your custom address type.
The following is a sample of the shipping rate processor code:
Create the JS model for the shipping address saving processor
This processor is responsible for sending the shipping address and the selected rate to the server.
In your custom module directory, create the component’s .js file for the processor. It must be located under the <your_module_dir>/view/frontend/web/js/model/ directory.
Following is a sample of the shipping rate processor code:
Create the JS component registering the processors
In your custom module directory, create the .js UI component that registers the rate processor and the saving processor. It must be located under the <your_module_dir>/view/frontend/web/js/view/ directory.
The file content must be similar to the following:
Declare the new components in the checkout page layout
In your custom module directory, create a new <your_module_dir>/view/frontend/layout/checkout_index_index.xml file. In this file, add the following:
The address_type you need to specify in the layout, is the value you set in the JS model of your custom address type.
Add the shipping address renderer to the “Ship-To” block (optional)
On the Review and Payment Information step of checkout, the shipping address is displayed in the Ship-To section for customer to make sure everything is set correctly.
If you want your custom address type to be displayed here as well, you need to create an .html template for rendering it, and declare in the corresponding location in layout.
Add template for displaying the address in the Ship-To section
In your custom module directory create a new <your_module_dir>/view/frontend/web/template/<your_template>.html file. The template can use Knockout JS syntax.