How can I add an icon next to the WooCommerce Checkout page Input
tag, which when tapped shows a Tooltip
with text?
How to edit checkout inputs
with hooks
in WooCommerce?
I want to add an icon inside the billing details, in the phone field input, which is billing_phone
, in the WooCommerce checkout, so that when we touch it or hover over it, the Tooltip
will display a text.
I can’t find the way to modify each input
, I have seen that I can add elements before the Billing section with the Hook woocommerce_before_checkout_billing_form
and after the Billing section with the following Hook woocommerce_after_checkout_billing_form
I EDIT THE QUESTION
With the help of @Mtxz , I managed to do tests and thanks to one of its functions, I managed to add the icon at checkout.
add_filter( 'woocommerce_form_field', 'add_code_to_checkout_phone_field', 10, 4 );
function add_code_to_checkout_phone_field( $field, $key, $args, $value ) {
if ( $key === 'billing_phone' ) {
$content = '<strong>
<div class="colocandolo">
<div class="tooltip top">
<img src="https://i.postimg.cc/ryJxTmp6/marron-Interro.png" width="20px"/>
<span class="tiptext">
Esto es un texto
</span>
</div>
</div>
</strong>';
$field = preg_replace('/</p>/', $content . '</p>', $field);
}
return $field;
}
the problem is that the icon is displayed at the top of the "Billing Details"
form, below the title.
You can see it in the following image:
What do I need to change in the @Mtxz function so that the icon is displayed next to the "Phone" label
?
I have tried this example to guide me in the creation of the function
to achieve what I am looking for, but it does not work
Can I add icons to the Checkout form labels
from the Hooks
?
Why doesn’t the function I created work?
I’ve searched but I can’t find the right hook to add the icon that shows a Tooltip next to the input labels.
If this can’t be done using Hooks
, and there isn’t a plugin that does it, maybe it can be done by editing a WooCommerce template, I imagine so.
What is the template that should be customized?
You can see what I’m looking for in any WooCommerce admin page, for example in wp-admin => settings => General .
All the fields have an icon that is a question mark and shows information in a Tooltip when we touch the icon.
I have also added a screenshot
https://ibb.co/5Y2k2PL
I have created a small demo with html and css, but I don’t know how I can get this implemented in WooCommerce
.colocandolo{
align-items: center;
display: flex;
flex-flow: column nowrap;
height: 100vh;
justify-content: center;
}
.tooltip {
position: relative;
display: inline-block;
/* border-bottom: 1px dotted rgb(175, 76, 6); */
}
.tooltip .tiptext {
visibility: hidden;
width: 120px;
background-color: rgb(93, 69, 4);
color: #fff;
text-align: center;
border-radius: 3px;
padding: 6px 0;
position: absolute;
z-index: 1;
box-shadow: 0 5px 10px rgb(93, 69, 4);
}
.tooltip .tiptext::after {
content: "";
position: absolute;
border-width: 5px;
border-style: solid;
}
.tooltip:hover .tiptext {
visibility: visible;
}
.tooltip.top .tiptext{
margin-left: -60px;
bottom: 150%;
left: 50%;
}
.tooltip.top .tiptext::after{
margin-left: -5px;
top: 100%;
left: 50%;
border-color: rgb(93, 69, 4) transparent transparent transparent;
}
<div class="colocandolo">
<div class="tooltip top">
<img src="https://i.postimg.cc/ryJxTmp6/marron-Interro.png" width="20px"/>
<span class="tiptext">
This is the text that we should display
</span>
</div>
</div>
6
Answers
I see two way to handle this :
I would add a custom class to the desired input. JS would be used to trigger the tooltip, and retrieve text from a custom HTML attribute.
This solution may not be compatible with your CSS only + custom HTML markup tooltip.
Woocommerce billing input cannot be override using a template file. They are generated from the
woocommerce_form_field()
method, which is called for every input in$checkout->get_checkout_fields( 'billing' )
(form-billing.php
).Looking at the
woocommerce_form_field()
code, I can see awoocommerce_form_field
hook allowing us to edit the final input HTML output.This code will edit the
billing_phone
HTML output, and add content before the</p>
(so at the end, but IN the input "top-level" container).You can add your tooltip here, and handle positioning with something like
position: relative
on the parent container, and absolute positioning on the tooltip.You can dump the "$field" value to check your input HTML, and adapt the regex part.
Note: I first tried to wrap the entire
$field
HTML in a custom<div>
. But it ended up breaking my form grid (which may be specific to my implementation on my test env). So I used a regex to inject the custom HTML code in the input container.See:
I modified(by appending a CSS class to the label and replacing the label content with an HTML icon and tooltip) your function as below:
I have tested the above on a woocommerce store and it works as expected.
Used CSS to hide it by default and show on hover.
ok, you can do this by adding the following code in your functions.php
This code is assuming you have FontAwesome icons pack and will display the icon next to your phone number. If you hover it, a custom text will appear like here.
You should see a icon or a "X". If you have font awesome, remove the x and you’ll have just the icon.
Enjoy.
try using this code in your theme’s
functions.php
to place theicon
:I didn’t fix your CSS if it was helpful I would be happy to help you with CSS too.
You can simply achieve this with pure jQuery and CSS
the default SVG title will show on hover, you can remove that if you want and use the span
.title
insteadIf you did not find a solution with
php
functions, you can do it withcss
, only instead of an icon, we add the question mark:"?"
, next to the"Phone" Label
.Or you can add inside the
input
, with your own example, and the icon, where you like the most.I have seen it both ways.
I hope it is useful