【问题标题】:Add a pop up modal to WooCommerce product "view more" button向 WooCommerce 产品“查看更多”按钮添加弹出模式
【发布时间】:2017-11-14 15:49:16
【问题描述】:

我正在开发一个 wordpress 网站,它有一个产品页面,在网格中列出产品。所有产品如下图所示。 products grid

我想知道:当单击查看更多按钮时,会弹出一个显示,其中有 2 列,左侧是产品完整描述和图像,右侧是一个表单,带有占位符和选项以及一个提交订单按钮。目标是在客户输入所有数据后单击 sumbit,产品以及已输入的所有数据作为电子邮件发送给管理员。

我自然不想要所有需要的代码,只想要工作流程。喜欢先做这个,然后做那个等等......

感谢所有帮助

【问题讨论】:

  • 您对需求的描述不够清楚,无法制定准确的步骤计划。但我认为您应该使用.on( 'click', '*view more button*') 触发 Ajax 调用。您的 Ajax 调用应该触发一个 PHP 函数,该函数返回您的产品详细信息弹出窗口所需的所有数据。这些数据将与一些 WooCommerce 函数一起提供,这些函数可以与 product_ID 一起使用,您可以将其存储在 show more button 或隐藏字段中。您可以使用 jQuery 将此数据附加到任何 div。
  • 非常感谢,我的问题的哪一部分不清楚?所以我可以更好地描述并得到更好的解决方案

标签: php jquery ajax wordpress woocommerce


【解决方案1】:

在这里,这应该足以让您入门。

首先你需要添加css、js元素到你的页面,你可以使用WordPress enqueue hook将它们分别添加到CSS和JS文件中,这只是一个基本的例子:

我将模态示例复制到:https://www.w3schools.com/howto/howto_css_modals.asp 以在此示例中使用。

function pop_up() {
?>
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <div class="content-left">
         <!-- Leave empty to be able to populate later with ajax -->
    </div>

    <div class="content-right">
         <!-- Put your form here -->
    </div>
  </div>

</div>
<style>
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content/Box */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}
</style>

<script>
jQuery(document).ready(function($) {
var modal = document.getElementById('myModal');
var span = document.getElementsByClassName("close")[0];

span.onclick = function() {
    modal.style.display = "none";
}
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
// Let's do the magic
    // make sure to change #myBtn to read more button selector
    $(document).on("click","#myBtn",function(e) {
    e.stopImmediatePropagation();
    e.preventDefault();
    // get url
        var url = $(this).attr('href');
        var container = $('#myModal').find('.content-left');
        var data = {
            action: 'show_product',
            url: url,

        };
        $.post('<?php echo esc_url( home_url() ); ?>/wp-admin/admin-ajax.php', data, function(response) {
           // display the response
           console.log(response);
           $(container).empty();       
           $(container).html(response);
            modal.style.display = "block";
        });
    });
});
</script>
<?php
}
add_action( 'wp_footer', 'pop_up' );

现在我们将根据 post 请求发送的 url 用 php 创建一个响应,您可以使用它来获取产品图像、内容、描述、价格或任何东西,并将它们放在一起以在 ajax 中以 HTML 形式返回响应以在模态中显示它

add_action('wp_ajax_show_product', 'show_product_callback_wp');
add_action( 'wp_ajax_nopriv_show_product', 'show_product_callback_wp' );
function show_product_callback_wp() {
     $url = $_POST['url'];
     $product_id = url_to_postid( $url );

    // product content
    $content_post = get_post($product_id);
    $content = $content_post->post_content;
    $content = apply_filters('the_content', $content);
    $content = str_replace(']]>', ']]&gt;', $content);

     $output = "";
     $output .= get_the_post_thumbnail( $product_id, 'medium');;
     $output .= $content;

     echo $output;
     exit(); 
}

这是经过测试和工作的。

【讨论】:

  • 感谢 Ali_k 的帮助。但我对wordpress开发有点陌生,我应该把你提到的这些代码放在哪里?问题是我不太了解wordpress流程,也不知道ajax和jquery,我需要知道如何组装这些代码!再次感谢
  • 这在你的functions.php中,在这里将按钮ID更改为你的按钮选择器并保存$(document).on("click","#myBtn",function(e) 然后你需要注意布局
  • 我正在使用 VC 后网格。在 VC 网格生成器中,按钮有一个 JS 功能选项(如图所示)我应该在这里放什么?图片:link
  • 这样保存并更改为:$(document).on("click",".myBtn",function(e)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2018-09-01
  • 2019-03-25
相关资源
最近更新 更多