skip to Main Content

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:

enter image description here

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

enter image description here

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>

enter image description here

6

Answers


  1. I see two way to handle this :

    1. Using CSS Class and JS

    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.

    add_filter( 'woocommerce_checkout_fields', 'customize_checkout_fields' );
    
    function customize_checkout_fields( $fields ) {
        $fields[ 'billing' ][ 'billing_phone' ][ 'class' ][]           = 'custom-class';
        $fields[ 'billing' ][ 'billing_phone' ][ 'custom_attributes' ] = [
            'data-text' => 'custom text'
        ];
    
        return $fields;
    }
    
    1. Edit Woocommerce HTML Output

    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 a woocommerce_form_field hook allowing us to edit the final input HTML output.

    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>Additional content</strong>';
            $field = preg_replace('/</p>/', $content . '</p>', $field);
        }
    
        return $field;
    }
    

    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:

    Login or Signup to reply.
  2. 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:

     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' ) {
            $args['label_class'] .= ' checkout-tooltip'; // add CSS class to label
    
            $content = '<img src="https://i.postimg.cc/ryJxTmp6/marron-Interro.png" width="20px" class="tooltip-icon"/>
                <span class="tooltip-text">Esto es un texto</span>';
    
            // add the icon and tooltip to the label
            $field = str_replace( '<label', '<label data-tooltip="' . esc_attr__( 'Phone tooltip', 'your-text' ) . '"', $field );
            $field = str_replace( '<label', '<label class="checkout-tooltip-label"', $field );
            $field = str_replace( '</label>', $content . '</label>', $field );
            $field = str_replace( '<input', '<input aria-describedby="billing_phone_tooltip"', $field );
        }
    
        return $field;
    }
    

    I have tested the above on a woocommerce store and it works as expected.
    enter image description here
    Used CSS to hide it by default and show on hover.

    Login or Signup to reply.
  3. ok, you can do this by adding the following code in your functions.php

    function add_phone_icon_tooltip( $field, $key, $args, $value ) {
        if ( $key === 'billing_phone' && is_checkout() ) {
            $icon = '<i class="fas fa-info-circle" data-toggle="tooltip" title="'. esc_attr__( 'Custom tooltip text goes here', 'textdomain' ) . '">X</i>';
            $field = str_replace( '</label>', ' ' . $icon . '</label>', $field );
        }
        return $field;
    }
    add_filter( 'woocommerce_form_field', 'add_phone_icon_tooltip', 10, 4 );
    

    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.

    Login or Signup to reply.
  4. try using this code in your theme’s functions.php to place the icon:

    add_filter( 'woocommerce_form_field', 'add_code_to_checkout_phone_field', 99, 4 );
    
    function add_code_to_checkout_phone_field( $field, $key, $args, $value ) {
        if ( $key == 'billing_phone' ) {
            $content = '<span class="tooltip top" style="display:inline-block">
             <img src="https://i.postimg.cc/ryJxTmp6/marron-Interro.png" width="20px"/>
              <span class="tiptext">
               Esto es un texto
                </span>
              </span>';
            $field = preg_replace('/</label>/ism',  "$content</label>", $field);
        }
    
        return $field;
    }
    

    I didn’t fix your CSS if it was helpful I would be happy to help you with CSS too.

    Login or Signup to reply.
  5. You can simply achieve this with pure jQuery and CSS

    add_action('wp_footer', function() {
        if ( is_checkout() ) { ?>
    <style>
        #billing_phone_field .woocommerce-input-wrapper {
            position: relative;
        }
        #billing_phone_field .woocommerce-input-wrapper .tooltip .icon, #billing_phone_field .woocommerce-input-wrapper .tooltip .title {
            position: absolute;
            right: 5px;
            top: 0;
        }
        #billing_phone_field .woocommerce-input-wrapper .tooltip .icon {
            width: 20px;
            height: 20px;
        }
        #billing_phone_field .woocommerce-input-wrapper .tooltip .title {
            top: 20px;
            background: #333;
            color: #fff;
            font-size: 12px;
            padding: 0 10px;
            border-radius: 5px;
            display: none;
        }
        /* #billing_phone_field .woocommerce-input-wrapper .tooltip .icon:hover + .title {
            display: block;
        } */
    </style>
    <script>
        (function($) {
    
            const title = 'This is the text that we should display'
            const toolTip = '<div class="tooltip"><svg class="icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>'+title+'</title><path d="M15.07,11.25L14.17,12.17C13.45,12.89 13,13.5 13,15H11V14.5C11,13.39 11.45,12.39 12.17,11.67L13.41,10.41C13.78,10.05 14,9.55 14,9C14,7.89 13.1,7 12,7A2,2 0 0,0 10,9H8A4,4 0 0,1 12,5A4,4 0 0,1 16,9C16,9.88 15.64,10.67 15.07,11.25M13,19H11V17H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12C22,6.47 17.5,2 12,2Z" /></svg><span class="title">'+title+'</span></div>'
    
            $('#billing_phone_field').find('.woocommerce-input-wrapper').append(toolTip) 
    
        })(jQuery)
    
    </script>
    <?php
        }
    });
    

    the default SVG title will show on hover, you can remove that if you want and use the span .title instead

    Login or Signup to reply.
  6. If you did not find a solution with php functions, you can do it with css, only instead of an icon, we add the question mark: "?", next to the "Phone" Label.

    p.form-row:has(>label[for="billing_phone"]) {
      position:relative;
     
     }
     
     p.form-row:has(>label[for="billing_phone"]):after{
      width: 22px;
      height: 22px;
      top: 1px;
      left: 79px;
      border-radius: 50%;
      background-color: #927124;
      content: "?";
      color: #F1EAD6;
      display: block;
      position: absolute;
      display: grid;
      justify-content: center;
      align-items: center;
      font-weight: 900;
     }
     
     
     p.form-row:has(>label[for="billing_phone"]):before{
       display: block;
       content: "This is the text that we should display";
       visibility: collapse;
       width: 120px;
       background-color: rgb(93, 69, 4);
       color: #fff;
       text-align: center;
       border-radius: 3px;
       padding: 10px;
       font-size:0.9em;
       position: absolute;
       z-index: 9990;
       left: 99px;
       top: -13px;
       box-shadow: 0 5px 10px rgb(93, 69, 4);
     }
     
     p.form-row:has(label[for="billing_phone"]):hover:before{
       visibility: visible;
     }
     p.form-row:has(label[for="billing_phone"]:hover, span:hover):before{
       visibility: collapse;
     }
    

    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

    label[for="billing_phone"] + span {
      position:relative;
     }
     
     label[for="billing_phone"] + span > input.input-text {
      padding-right:34px;
     }
     
     label[for="billing_phone"] + span:before{
       width: 24px;
       height: 24px;
       top: -7px;
       right: 8px;
       
       content: "";
        display: block;
       background: url("ttps://i.postimg.cc/ryJxTmp6/marron-Interro.png") no-repeat 0 0;
       background-size: 100%;
       position:absolute;
     }
     
     
     label[for="billing_phone"] + span:after{
       display: block;
       content: " El número de teléfono lo necesitamos por si tuviéramos problemas con el pedido";
       visibility: hidden;
       width: 120px;
       background-color: rgb(93, 69, 4);
       color: #fff;
       text-align: center;
       border-radius: 3px;
       padding: 10px;
       font-size:0.9em;
       position: absolute;
       z-index: 9990;
       right:2px;
       top:8px;
       box-shadow: 0 5px 10px rgb(93, 69, 4);
     }
     
     label[for="billing_phone"] + span:hover:has(>input:not(:hover)):after{
        visibility: visible;
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search