Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

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!

question from:https://stackoverflow.com/questions/66053315/add-tooltip-to-flat-rate-shipping-method-in-woocommerce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...