通过模板覆盖为WooCommerce类别布局加载更多或查看更多功能

时间:2016-08-13 作者:Arif

在我的WoodPress woocommerce中,我设计了我的类别布局,只显示带有相应标题和链接的图像。

现在

我想在不使用任何插件的情况下集成ajax加载更多功能ajax load more. 我如何才能为WooCommerce实现这一目标Load More 或View More 按钮,显示所有产品

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

您可以通过为woocommerce类别布局创建模板覆盖来实现这一点。

如何创建模板覆盖转到wp-content/theme/{your_theme}woocommerce 如果已经不存在,则创建一个名为archive-product.php
  • 现在,按照说明粘贴以下代码,并根据需要修改任何参数,然后立即添加此代码if ( ! defined( \'ABSPATH\' ) ) { exit; // Exit if accessed directly }

    /** Load More or View More Functionality 
     * What it is doing is checking if an ajax request was fired
     * and if fired, will run the code and exit to terminate the ajax request
     */ 
    
        if ($_POST[\'start\'] || $_POST[\'load\']) {
    
            $bool = get_queried_object();
            $parent_cat_NAME = $bool->name;
            $subCatList = $bool->name;
            $IDbyNAME = get_term_by(\'name\', $parent_cat_NAME, \'product_cat\');
            $product_cat_ID = $IDbyNAME->term_id;
            $args = array(
               \'hierarchical\' => 1,
               \'show_option_none\' => \'\',
               \'hide_empty\' => 0,
               \'parent\' => $product_cat_ID,
               \'taxonomy\' => \'product_cat\'
            );
            $subcats = get_categories($args);
            $total = count($subcats);
              foreach ($subcats as $sc) {
                $link = get_term_link( $sc->slug, $sc->taxonomy );
                  $subCatList  = $subCatList.\', \'.$sc->slug;
              }
                $args = array( \'post_type\' => \'product\', \'posts_per_page\' => $_POST[\'load\'], \'product_cat\' => ($subCatList ? $subCatList : $bool->slug), \'orderby\' => \'title\', \'order\' => \'ASC\', \'offset\' => (($_POST[\'start\'] + $_POST[\'load\']) - $_POST[\'load\']) );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
    
                        <li class="product type-product status-publish has-post-thumbnail">    
    
                        <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
    
                                <?php woocommerce_show_product_sale_flash( $post, $product ); ?>
    
                                <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, \'shop_catalog\'); else echo \'<img src="\'.woocommerce_placeholder_img_src().\'" alt="Placeholder" width="170px" height="232px" />\'; ?>              
    
                            </a>
                                <h3><?php the_title(); ?></h3>                          
    
                        </li>
    
                <?php endwhile; ?>
            <?php wp_reset_query(); ?>
    <?php exit(); } ?>
    <?php /* Load More or View More Functionality */ ?>
    
    现在在后面添加下面的代码do_action( \'woocommerce_sidebar\' );

    /* Count Total Products */
    
                $bool = get_queried_object();
                $parent_cat_NAME = $bool->name;
                $subCatList = $bool->name;
                $IDbyNAME = get_term_by(\'name\', $parent_cat_NAME, \'product_cat\');
                $product_cat_ID = $IDbyNAME->term_id;
                $args = array(
                   \'hierarchical\' => 1,
                   \'show_option_none\' => \'\',
                   \'hide_empty\' => 0,
                   \'parent\' => $product_cat_ID,
                   \'taxonomy\' => \'product_cat\'
                );
                $subcats = get_categories($args);
                $total = count($subcats);
                  foreach ($subcats as $sc) {
                    $link = get_term_link( $sc->slug, $sc->taxonomy );
                      $subCatList  = $subCatList.\', \'.$sc->slug;
                  }
                  $args = array( \'post_type\' => \'product\', \'posts_per_page\' => 1000, \'product_cat\' => ($subCatList ? $subCatList : $bool->slug), \'orderby\' => \'title\', \'order\' => \'ASC\' );
                    $loop = new WP_Query( $args );
                    $count = 0;
                    while ( $loop->have_posts() ) : $loop->the_post(); global $product; 
                        $count ++; 
    
                    endwhile; 
                    wp_reset_query();
    
        /* Count Total Products */
    
        ?>
        <button class="btn btn-load-more" onclick="load_more(<?php echo $count; ?>);">View More</button>
    
        <script type="text/javascript">
            var start = 15; /* Set the offset value i.e the number of products to skip */
            var load = 20; /* How many products you want to load in one load */
            var current = jQuery(\'ul.products li\').length;
    
            if(start > current) { jQuery(\'button.btn.btn-load-more\').attr("disabled",true);}
    
            function load_more(max) {
                 jQuery.ajax({
                  type: "POST",
                  url: window.location.pathname,
                  data: { start: start, load: load }
    
                }).done(function( msg ) {
                  jQuery(\'ul.products\').append(msg);
                  start = start + load;
                    if(start>max) { jQuery(\'button.btn.btn-load-more\').attr("disabled",true);}
                });
            };
        </script>