2

I am trying to figure out how to customize the checkout form by changing classes because I want to stylize it with bootstrap instead of the default Magento styles. I don't see them defined anywhere and with my reading and looking at the Magento developer documentation it looks like they are dynamically generated http://devdocs.magento.com/guides/v2.0/howdoi/checkout/checkout_new_field.html But in that documentation it doesn't look like there is any way of adding custom classes to the wrapper elements or anything.

This is the default output for the First Name:

<div class="field _required" data-bind="visible: visible, attr: {'name': element.dataScope}, css: additionalClasses" name="billingAddresscheckmo.firstname">
 <label class="label" data-bind="attr: { for: element.uid }" for="DPGHTAP">
 <!-- ko if: element.label -->
 <span data-bind="text: element.label">First Name</span>
 <!-- /ko -->
 </label>
 <div class="control" data-bind="css: {'_with-tooltip': element.tooltip}">
 <!-- ko ifnot: element.hasAddons() -->
 <!-- ko template: element.elementTmpl -->
<input class="input-text" type="text" data-bind="
 value: value,
 valueUpdate: 'keyup',
 hasFocus: focused,
 attr: {
 name: inputName,
 placeholder: placeholder,
 'aria-describedby': noticeId,
 id: uid,
 disabled: disabled
 }" name="firstname" placeholder="" aria-describedby="notice-DPGHTAP" id="DPGHTAP">
<!-- /ko -->
 <!-- /ko -->
 <!-- ko if: element.hasAddons() --><!-- /ko -->
 <!-- ko if: element.tooltip --><!-- /ko -->
 <!-- ko if: element.notice --><!-- /ko -->
 <!-- ko if: element.error() --><!-- /ko -->
 <!-- ko if: element.warn() --><!-- /ko -->
 </div>
</div>

I would prefer to have an output similar to this:

<div class="form-group field _required" data-bind="visible: visible, attr: {'name': element.dataScope}, css: additionalClasses" name="billingAddresscheckmo.firstname">
 <label class="label" data-bind="attr: { for: element.uid }" for="DPGHTAP">
 <!-- ko if: element.label -->
 <span data-bind="text: element.label">First Name</span>
 <!-- /ko -->
 </label>
 <div class="control" data-bind="css: {'_with-tooltip': element.tooltip}">
 <!-- ko ifnot: element.hasAddons() -->
 <!-- ko template: element.elementTmpl -->
<input class="form-control input-text" type="text" data-bind="
 value: value,
 valueUpdate: 'keyup',
 hasFocus: focused,
 attr: {
 name: inputName,
 placeholder: placeholder,
 'aria-describedby': noticeId,
 id: uid,
 disabled: disabled
 }" name="firstname" placeholder="" aria-describedby="notice-DPGHTAP" id="DPGHTAP">
<!-- /ko -->
 <!-- /ko -->
 <!-- ko if: element.hasAddons() --><!-- /ko -->
 <!-- ko if: element.tooltip --><!-- /ko -->
 <!-- ko if: element.notice --><!-- /ko -->
 <!-- ko if: element.error() --><!-- /ko -->
 <!-- ko if: element.warn() --><!-- /ko -->
 </div>
</div>

Notice how there is now a form-group and form-control class added.

How does one accomplish this if everything is dynamically generated?

Rakesh Jesadiya
42.5k19 gold badges135 silver badges186 bronze badges
asked Feb 25, 2017 at 19:26
1

1 Answer 1

2
+50

For set class for each input field in shipping form,

By default Main div comes from the core file, vendor/magento/module-ui/view/frontend/web/templates/form/field.html

You have to override above core file into your theme, For add form-group class to the main div just override file at your theme location,

app/design/frontend/{Namespace}/{themename}/Magento_Ui/web/templates/form/field.html

changes:

<div class="form-group field" data-bind="visible: visible, attr: {'name': element.dataScope}, css: additionalClasses">
</div>

All input field comes from the core file,

vendor/magento/module-ui/view/frontend/web/templates/form/element/input.html,

You have to override above core file into your theme, For add form-control class to input just override file at your theme location,

app/design/frontend/{Namespace}/{themename}/Magento_Ui/web/templates/form/element/input.html

Code for input.html is like below,

<input class="form-control input-text" type="text" data-bind="
 value: value,
 valueUpdate: 'keyup',
 hasFocus: focused,
 attr: {
 name: inputName,
 placeholder: placeholder,
 'aria-describedby': noticeId,
 id: uid,
 disabled: disabled
 }" />

Clear Browser cache and clear system cache.

Run php bin/magento setup:static-content:deploy command.

Utsav Gupta
1,2431 gold badge20 silver badges52 bronze badges
answered Mar 7, 2017 at 4:59
3
  • 3
    I think above changes apply to all over. We need to only apply on Checkout Page Commented Mar 7, 2017 at 8:45
  • As i understand, the path should be app/design/frontend/{Namespace}/{themename}/Magento_Ui/..., not Module_Ui, no? Commented Jul 19, 2017 at 12:23
  • @Rakesh, any thoughts on Jackson remarks? Commented Jan 9, 2021 at 20:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.