In this article we will work a bit on Magento 2 checkout and create a new feature - adding a comment box in shipping sections. The use case is very simple - often times merchants want to allow customers to enter comments regarding their shipping choice. In native Magento this is not possible. This will likely turn out to be a series of tutorials. We will start at the very base - getting the component to render and set the structure for being able to process the data later on. Let's get started.
First you will need to create a module. I assume you are familiar with that because if you are not you will likely not be able to follow the next steps. You might want to look through our other articles before starting with this one.
TIP: A lot of Magento code can be generated using Pestle
Once you have the module created you will want to enable it and make sure you have your dev environment all set up.
Add the component
Add the component after shipping methods in checkout layout. We are going to use native shippingAdditional node which will render our component as part of the shipping step. Add the component as a child to this node:
Once we have the component added in layout we need to define the component class Techflarestudio_ShippingComment/js/view/checkout/shipping-comment-block.
A few things to mention regarding this:
We are defining our template file
We are using ko.computed to define an observable value. This will automatically determine the currently selected shipping method. This is not required in order to show the block for all shipping methods. However we are adding this here in order to demonstrate how the block can be added to a set or a specific shipping method. We will use the selectedMethod() function in the template to determine if the block should be visible.
As the last step for displaying the component you need to create the template itself.
As you can see we are starting with the observable function to check if the block should be visible. We are setting this block to be visible for our custom free shipping method (take a look in this article how to create a custom shipping carrier).
We are also using a collapsible widget which allows us to toggle the comment box and by default show only the title Add a shipping comment.
The form is simple - a single textarea field that allows the user to enter the comment.
At this point if you followed along correctly you should be able to see the comment box appear below the shipping methods. If you are having issues make sure that the selected shipping method matches the computed value.
Process the data
Before we can save this data we need to create a base for passing the form values and passing them to appropriate classes. First you will need to declare a mixin for place order action. We only care about the place order action because the comment is not relevant for us before the order is placed. If you care about saving this value for abandoned carts then you may want to hook into set shipping information or similar.
Next we will create our mixin class.
The mixin is very simple - it only wraps the original method and calls our custom data processor which will handle the data. And finally create the processor which will add our comment to payment extension attributes.
The processor is fetching and setting the comment value for payment extension attributes. At this point the data is not yet saved however we have the base of data processing flow and we are ready to create the backend part of this task.
That is it for this article. In the next one we will show you how to save this value to database and how to show it in admin order view. Stay tuned.