【问题标题】:WooCommerce related products - Load more (ajax)WooCommerce 相关产品 - 加载更多 (ajax)
【发布时间】:2020-10-22 19:59:17
【问题描述】:

我有一个 WooCommerce 商店,我们需要有很多相关产品。我们希望只显示 4 开头,然后是“显示更多”功能。我宁愿不只是隐藏产品然后用 jQuery 显示,而是用 ajax 动态加载它们。

我已经搜索了类似的解决方案,但找不到任何东西。所以我希望有一些可以帮助我?

【问题讨论】:

    标签: ajax wordpress woocommerce


    【解决方案1】:

    对于 index.php 文件:

    <?php
           include 'dbh.php';
         ?>
        <!DOCTYPE html>
        <html>
        <head>
          <link rel="stylesheet" type="text/css" href="style.css">
          <script src="index.js"></script>
          <script src="jquery-3.5.1.min.js"></script>
        </head>
        <body>
          <div id="products">
            <?php
            $sql = "SELECT * FROM products LIMIT 2";
            $result = mysqli_query($conn, $sql);
            if(mysqli_num_rows($result) > 0){
              while ($row = mysqli_fetch_assoc($result)) {
                echo "<p>";
                echo $row['product'];
                echo "<br>";
                echo $row['product_name'];
                echo "</p>";
              }
            } else {
              echo "There are no more products.";
            }
             ?>
          </div>
          <button id="btn">Show more products</button>
        </body>
        </html>
    
    
    
    <!-- language: lang-js -->
    
        $(document).ready(function(){
          var productCount = 2;
            $("btn").click(function() {
                productCount += 2;
              $("#data-placeholder").load("loaded_data.php", {
                productNewCount: productCount
              });
            });
        });
    
    <!-- end snippet -->
    
    enter code hereYou need to to use multiple files in Ajax, one where the data loads that you can call loaded_data.php that is going to to get added to the main php file (example: index.php),
    
        <?php
        include 'dbh.php';//dbh.php is the file that hold the database connection.
    
        $productNewCount = $POST['productNewCount'];
    
        $sql = "SELECT * FROM products LIMIT $productNewCount";
        $result = mysqli_query($conn, $sql);
        if(mysqli_num_rows($result) > 0){
          while ($row = mysqli_fetch_assoc($result)) {
            echo "<p>";
            echo $row['product_name'];
            echo "<br>";
            echo $row['product'];
            echo "</p>";
          }
        } else {
          echo "There are no more products!";
        }
         ?>
    

    【讨论】:

      猜你喜欢
      • 2019-02-10
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      相关资源
      最近更新 更多