如何在每个循环产品的商店页面中创建自定义弹出窗口?

时间:2021-11-16 作者:Dhruv Suthar

我正在尝试为woocommerce商店页面中的所有loop产品创建自定义弹出框。但它不起作用,我认为它需要一些代码更正,

功能。PHP代码

add_action( \'woocommerce_after_shop_loop_item\', \'product_visibility_button\', 5 );
function product_visibility_button() {

    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        if ( in_array( \'administrator\', (array) $user->roles ) ) {
            ?>
            <button type="button" class="button" id="but" style="margin:10px" >Open Popup</button>
            <div style="display: none;" class="pop-outer">
            <div class="pop-inner">
                <button class="close">X</button>
                <h2>This is a custom pop-up example</h2>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
        </div>
            <?php
        }
    }
}
jquery代码

           $(document).ready(function (){
                $(".open").click(function (){
                    $(".pop-outer").fadeIn("slow");
                });
                $(".close").click(function (){
                    $(".pop-outer").fadeOut("slow");
                });
            });
CSS

.pop-outer {
            background-color: rgba(0, 0, 0, 0.5);
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }
        .pop-inner {
            background-color: #fff;
            width: 500px;
            height: 300px;
            padding: 25px;
            margin: 5% auto;
        }
另请参见随附的屏幕截图enter image description here

是否可以显示所有产品?

1 个回复
最合适的回答,由SO网友:Tanmay Patel 整理而成

Use below code. Tested and Worked perfectly.

功能。php

add_action( \'woocommerce_after_shop_loop_item\', \'product_visibility_button\', 5 );
function product_visibility_button() {
    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
        if ( in_array( \'administrator\', (array) $user->roles ) ) {
            ?>
            <button type="button" class="button" id="but" style="margin:10px" >Open Popup</button>
            <div style="display: none;" class="pop-outer">
            <div class="pop-inner">
                <button class="close">X</button>
                <h2>This is a custom pop-up example</h2>
                <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            </div>
        </div>
            <?php
        }
    }
}
jQuery代码

jQuery(document).ready(function (){
    jQuery(".button").click(function (){
        jQuery(this).next().fadeIn("slow");
    });
    jQuery(".close").click(function (){
        jQuery(this).closest(".pop-outer").fadeOut("slow");
    });
});
CSS代码

.pop-outer {
    background-color: rgba(0, 0, 0, 0.5);
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
}
.pop-inner {
    background-color: #fff;
    width: 500px;
    height: auto;
    padding: 25px;
    margin: 5% auto;
}

相关推荐

当in_the_loop()为假时,何时以及为什么is_Single(‘my_cpt’)为真?

我正在使用模板系统的示例代码。此地址的页码:http://project.test/my_cpt/hello-post/.无法理解原因is_singular( \'my_cpt\' ) 是true 虽然in_the_loop() 是false.在页面模板中The Loop "E;“工程”:if ( have_posts() ) { while ( have_posts() ) { the_post(); ?>