I’m using woocommerce for the first time to create an e-commerce wordpress powered website. I need to apply the bootstrap 4 css form classes and remove the default classes that woocommerce will use to wrap the form elements. Using the inspector I’ve noticed that a <p class="form-row form-row-first">
tag is added before every <label>
and <input>
, also the inputs are wrapped inside a <span>
tag and this will break all the bootstrap 4 layout. After some headche I’ve found a similar question here on SO, and I decided to give a try, but the solution will not work for me as I expect.
What I want to do is to create a form that respect the css grid layout of bootstrap like this:
<form>
<div class="form-group">
// form group wrapper for labels and inputs
<label for=""></label>
<input type="text" class="form-control" />
</div>
</div>
This is the basic markup for a bootstrap 4 form. For some fields I want also to use the .form-row
to group two fields like name and last name or similar into columns .col-sm-12 col-md-6 col-lg-6
.
Is this possible by using filters or hooks? I’ve found the core file that manage the fileds rendering function, but I don’t want to edit the file to avoid messing up the code.
Now the billing and checkout form fields have this structure:
Thanks for the help.
2
Answers
WooCommerce provide a way to override templates from your theme.
If you need to use the Bootstrap HTML markup and CSS classes, you will probably need to override WooCommerce templates following instructions given on WooCommerce documentation:
https://docs.woocommerce.com/document/template-structure/
If you are using a theme that you didn’t built yourself, the best practice is to create a child theme so you can override WooCommerce templates inside your child theme without altering the original and keep the ability to update it.
https://developer.wordpress.org/themes/advanced-topics/child-themes/
If you use Bootstrap 4 and SASS you can make it with @extend
For example you can easily change the default woocommerce button to a bootstrap button like this: