linccce

Pagination and results count problem.

Jun 25th, 2015
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.07 KB | None | 0 0
  1. //My problem with pagination:
  2.  
  3. //First I have this category template (archive-products.php, from woocommerce):
  4. <?php
  5. /**
  6.  * The Template for displaying product archives, including the main shop page which is a post type archive.
  7.  *
  8.  * Override this template by copying it to yourtheme/woocommerce/archive-product.php
  9.  *
  10.  * @author        WooThemes
  11.  * @package    WooCommerce/Templates
  12.  * @version     2.0.0
  13.  */
  14.  
  15. if (!defined('ABSPATH')) {
  16.     exit; // Exit if accessed directly
  17. }
  18.  
  19. get_header('shop'); ?>
  20.  
  21. <?php
  22. /**
  23.  * woocommerce_before_main_content hook
  24.  *
  25.  * @hooked woocommerce_output_content_wrapper - 10 (outputs opening divs for the content)
  26.  * @hooked woocommerce_breadcrumb - 20
  27.  */
  28. do_action('woocommerce_before_main_content');
  29. ?>
  30.  
  31. <?php if (apply_filters('woocommerce_show_page_title', true)) : ?>
  32.  
  33.     <h1 class="page-title" <?php echo !is_singular() && (is_tax('product_cat') || is_tax('product_tag')) ? 'style="display:block"' : ''; ?>><?php woocommerce_page_title(); ?></h1>
  34.  
  35. <?php endif; ?>
  36.  
  37. <?php do_action('woocommerce_archive_description'); ?>
  38.  
  39. <?php if (have_posts()) :
  40.  
  41.     global $wp_query; // I call $wp_query so I could change posts_found attribute, so pagination would be recalculated with new results
  42.  
  43.     /*echo '<pre>'; // This is for correct handling of newlines
  44.     ob_start();
  45.     var_dump($wp_query->query_vars['posts_per_page']); //20
  46.     $a=ob_get_contents();
  47.     ob_end_clean();
  48.     echo htmlspecialchars($a,ENT_QUOTES); // Escape every HTML special chars (especially > and < )
  49.     echo '</pre>';*/
  50.  
  51.     /**
  52.      * woocommerce_before_shop_loop hook
  53.      *
  54.      * @hooked woocommerce_result_count - 20
  55.      * @hooked woocommerce_catalog_ordering - 30
  56.      */
  57.     do_action('woocommerce_before_shop_loop');
  58.  
  59.     echo $wp_query->found_posts;
  60.  
  61.     woocommerce_product_loop_start();
  62.  
  63.     woocommerce_product_subcategories();
  64.  
  65.     global $product_categories_global_total; // Get the total number of subcategories in a category
  66.  
  67.     $i = 0;
  68.  
  69.     while (have_posts()) : the_post();
  70.  
  71.         if ($wp_query->queried_object->term_id == wp_get_post_terms(get_the_ID(), 'product_cat')[0]->term_id):
  72.  
  73.             wc_get_template_part('content', 'product');
  74.  
  75.             $i++;
  76.  
  77.         endif;
  78.  
  79.     endwhile; // end of the loop.
  80.  
  81.     $wp_query->found_posts = $product_categories_global_total+$i; // I get this one from customized subcategories function Look at the function below this template
  82.  
  83.     woocommerce_product_loop_end();
  84.  
  85.     /**
  86.      * woocommerce_after_shop_loop hook
  87.      *
  88.      * @hooked woocommerce_pagination - 10
  89.      */
  90.     do_action('woocommerce_after_shop_loop');
  91.  
  92. elseif (!woocommerce_product_subcategories(array('before' => woocommerce_product_loop_start(false), 'after' => woocommerce_product_loop_end(false)))) : ?>
  93.  
  94.     <?php wc_get_template('loop/no-products-found.php'); ?>
  95.  
  96. <?php endif; ?>
  97.  
  98. <?php
  99. /**
  100.  * woocommerce_after_main_content hook
  101.  *
  102.  * @hooked woocommerce_output_content_wrapper_end - 10 (outputs closing divs for the content)
  103.  */
  104. do_action('woocommerce_after_main_content');
  105. ?>
  106.  
  107. <?php
  108. /**
  109.  * woocommerce_sidebar hook
  110.  *
  111.  * @hooked woocommerce_get_sidebar - 10
  112.  */
  113. do_action('woocommerce_sidebar');
  114. ?>
  115.  
  116. <?php get_footer('shop'); ?>
  117.  
  118. // The customized subcategories function
  119.  
  120. $product_categories_global_total = 0;
  121. function woocommerce_product_subcategories( $args = array() ) {
  122.     global $woocommerce, $wp_query, $_chosen_attributes;
  123.  
  124.     $defaults = array(
  125.         'before'  => '',
  126.         'after'  => '',
  127.         'force_display' => false
  128.     );
  129.  
  130.     $args = wp_parse_args( $args, $defaults );
  131.  
  132.     extract( $args );
  133.  
  134.     // Main query only
  135.     if ( ! is_main_query() && ! $force_display ) return;
  136.  
  137.     // Don't show when filtering
  138.     if ( sizeof( $_chosen_attributes ) > 0 || ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) ) return;
  139.  
  140.     // Don't show when searching or when on page > 1 and ensure we're on a product archive
  141.     if ( is_search() || is_paged() || ( ! is_product_category() && ! is_shop() ) ) return;
  142.  
  143.     // Check cateogries are enabled
  144.     if ( is_product_category() && get_option( 'woocommerce_show_subcategories' ) == 'no' ) return;
  145.     if ( is_shop() && get_option( 'woocommerce_shop_show_subcategories' ) == 'no' ) return;
  146.  
  147.     // Find the category + category parent, if applicable
  148.     if ( $product_cat_slug = get_query_var( 'product_cat' ) ) {
  149.         $product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat' );
  150.         $product_category_parent = $product_cat->term_id;
  151.     } else {
  152.         $product_category_parent = 0;
  153.     }
  154.  
  155.     // NOTE: using child_of instead of parent - this is not ideal but due to a WP bug ( http://core.trac.wordpress.org/ticket/15626 ) pad_counts won't work
  156.     $args = array(
  157.         'child_of'      => $product_category_parent,
  158.         'menu_order'    => 'ASC',
  159.         'hide_empty'    => 0,
  160.         'hierarchical'  => 1,
  161.         'taxonomy'      => 'product_cat',
  162.         'pad_counts'    => 1
  163.     );
  164.     $product_categories = get_categories( $args  );
  165.  
  166.     $product_category_found = false;
  167.  
  168.     if ( $product_categories ) {
  169.  
  170.         global $product_categories_global_total;
  171.  
  172.         foreach ( $product_categories as $category ) {
  173.  
  174.             if ( $category->parent != $product_category_parent ){
  175.                 continue;
  176.             } else {
  177.                 $product_categories_global_total++;
  178.             }
  179.  
  180.  
  181.             if ( ! $product_category_found ) {
  182.                 // We found a category
  183.                 $product_category_found = true;
  184.                 echo $before;
  185.             }
  186.  
  187.             wc_get_template( 'content-product_cat.php', array(
  188.                 'category' => $category
  189.             ) );
  190.  
  191.         }
  192.  
  193.     }
  194.  
  195.     // If we are hiding products disable the loop and pagination
  196.     if ( $product_category_found == true && get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) {
  197.         $wp_query->post_count = 0;
  198.         $wp_query->max_num_pages = 0;
  199.     }
  200.  
  201.     if ( $product_category_found ) {
  202.         echo $after;
  203.         return true;
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment