php - Add tooltip to "flat rate" shipping method in WooCommerce

I want to make a tooltip for the "Flat Rate" delivery option. I need to put the "Question mark" icon next to "Flat Rate" and when I click on it, a prompt should tooltip with a description of the delivery terms.
I have a code that shows an icon, which pops up a shows when clicked.
<div class="popup" onclick="myFunction()"><i class="pe-7s-help1"></i>
<span class="popuptext" id="myPopup">
<?php if ( is_active_sidebar( 'tooltip_sidebar' ) ) : ?>
<div id="tooltip-sidebar" class="sidebar">
<?php dynamic_sidebar( 'tooltip_sidebar' ); ?>
</div>
<?php endif; ?>
</span>
</div>
I am also registering a new sidebar in which I will write the shipping terms.
add_action( 'widgets_init', 'register_new_sidebar' );
function register_new_sidebar() {
register_sidebar(
array(
'id' => 'tooltip_sidebar',
'name' => 'Tooltip Sidebar',
'description' => '',
'before_widget' => '<div id="%1$s" class="tooltip widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);
}
CSS style for a tooltip:
/* Popup container */
.popup {
position: relative;
display: inline-block;
cursor: pointer;
}
/* The actual popup (appears on top) */
.popup .popuptext {
visibility: hidden;
width: 320px;
background: #fff;
color: #222;
text-align: center;
padding: 10px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
-webkit-box-shadow: 0px 5px 15px 0px rgba(50, 50, 50, 0.3);
-moz-box-shadow: 0px 5px 15px 0px rgba(50, 50, 50, 0.3);
box-shadow: 0px 5px 15px 0px rgba(50, 50, 50, 0.3);
}
/* Popup arrow */
.popup .popuptext::after {
content: "X";
position: absolute;
top: 0;
right: 5%;
border-width: 5px;
border-style: solid;
border-color: #fff transparent transparent transparent;
}
/* Toggle this class when clicking on the popup container (hide and show the popup) */
.popup .show {
visibility: visible;
-webkit-animation: fadeIn 1s;
animation: fadeIn 1s
}
/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes fadeIn {
from {opacity: 0;}
to {opacity:1 ;}
}
Script for a tooltip:
<script>
// When the user clicks on <div>, open the popup
function myFunction() {
var popup = document.getElementById("myPopup");
popup.classList.toggle("show");
}
</script>
Tell me how this icon can be added to the "Flat Rate" delivery option using hooks in the Functions.php file? I just don't want to add this code to WooCommerce templates. And how can you make this code work in the cart and on the checkout page?
I will be glad for your help!
Answer
Solution:
Thanks @7uc1f3r for the help! I needed to replace my HTML code with this one:
function action_woocommerce_after_shipping_rate( $method, $index ) {
// Compare (adjust as needed)
if( $method->get_id() == 'flat_rate:1' ) {
echo '<span class="popup" onclick="myFunction()"><i class="pe-7s-help1"></i><span class="popuptext" id="myPopup">';
dynamic_sidebar( 'tooltip_sidebar' );
echo '</span></span>';
}
}
add_action( 'woocommerce_after_shipping_rate', 'action_woocommerce_after_shipping_rate', 10, 2 );
Add the code to your child theme's functions.php file. The code has been tested and works.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: http failure during parsing for
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.