在 WooCommerce 中向“统一费率”运输方式添加工具提示

时间:2021-02-04 20:25:26

标签: php wordpress woocommerce hook-woocommerce shipping-method

我想为“统一费率”交付选项制作一个工具提示。我需要将“问号”图标放在“统一费率”旁边,当我点击它时,提示应该带有交货条款描述的工具提示。

我有一个显示图标的代码,点击时会弹出一个显示。

<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>

我还注册了一个新的侧边栏,我将在其中写下运输条款。

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 样式:

/* 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>
// When the user clicks on <div>, open the popup
function myFunction() {
  var popup = document.getElementById("myPopup");
  popup.classList.toggle("show");
}
</script>

告诉我如何使用 Functions.php 文件中的钩子将此图标添加到“统一费率”交付选项中?我只是不想将此代码添加到 WooCommerce 模板中。以及如何使此代码在购物车和结帐页面上运行?

很高兴为您提供帮助!

1 个答案:

答案 0 :(得分:0)

感谢@7uc1f3r 的帮助!我需要用这个替换我的 HTML 代码:

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 ); 

将代码添加到您的子主题的functions.php 文件中。代码已经过测试并且可以正常工作。