Web Development Woocommerce Wordpress

How to implement category pagination in woocommerce

Update the below code in yourtheme/woocommerce/archive-product.php file to get pagination on category listing on default product page.

=========================

<?php
if ( ! defined( ‘ABSPATH’ ) ) {
exit; // Exit if accessed directly
}
get_header( ‘shop’ );
?>
<?php
if(is_shop()){
?>
<div class=””>
<?php
$base_thumb = wc_get_page_id( ‘shop’ );
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $base_thumb ), “full” );
?>
<img src=”<?php echo $thumbnail[0]; ?>”>
</div>
<?php } ?>
<div class=”container product_ab”>
<?php
/**
 * woocommerce_before_main_content hook.
 *
 * @hooked woocommerce_output_content_wrapper – 10 (outputs opening divs for the content)
 * @hooked woocommerce_breadcrumb – 20
 * @hooked WC_Structured_Data::generate_website_data() – 30
 */
do_action( ‘woocommerce_before_main_content’ ); ?>
<?php if ( apply_filters( ‘woocommerce_show_page_title’, true ) ) : ?>
<h1 class=”woocommerce-products-header__title page-title”><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
<div class=”sidebar-shop”>
<?php
/**
 * woocommerce_sidebar hook.
 *
 * @hooked woocommerce_get_sidebar – 10
 */
do_action( ‘woocommerce_sidebar’ );
?>
</div>
<div class=”product_cover”>
<header class=”woocommerce-products-header”>
<div class=”wl-banner-img”>
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$image = get_field(‘product_category_banner_image’, $taxonomy . ‘_’ . $term_id);
$image = wp_get_attachment_image_src($image,’full’);
if(isset($image[0])){ ?>
<img src=”<?php echo $image[0]; ?>”>
<?php } ?>
<div class=”wl-desc”><?php the_field(‘product_category_banner_description’,$taxonomy . ‘_’ .$term_id); ?></div>
</div>
<?php
/**
 * woocommerce_archive_description hook.
 *
 * @hooked woocommerce_taxonomy_archive_description – 10
 * @hooked woocommerce_product_archive_description – 10
 */
do_action( ‘woocommerce_archive_description’ );
?>
</header>
<?php
if ( have_posts() ) :
/**
 * woocommerce_before_shop_loop hook.
 *
 * @hooked wc_print_notices – 10
 * @hooked woocommerce_result_count – 20
 * @hooked woocommerce_catalog_ordering – 30
 */
do_action( ‘woocommerce_before_shop_loop’ );
?>
<?php woocommerce_product_loop_start(); ?>
<?php //woocommerce_product_subcategories(); ?>
<?php
/****************************/
//get_query_var to get page id from url
$page = ( get_query_var(‘paged’) ) ? get_query_var(‘paged’) : 1;
// number of tags to show per-page
$per_page = 6;
$parentid = get_queried_object_id();
if($page == 1){
$offset = 0;
}else{
$offset = ( $page – 1 ) * $per_page;
}
$args = array(
    ‘orderby’      => ‘name’,
    ‘count’    => 1,
    ‘pad_counts’   => 0,
    ‘hierarchical’ => 1,
    ‘title_li’     => ”,
    ‘hide_empty’   => 0,
    ‘parent’  => $parentid
);
$term_args = array(
    ‘number’ => $per_page,
    ‘offset’ => $offset,
    ‘orderby’      => ‘name’,
    ‘count’    => 1,
    ‘pad_counts’   => 0,
    ‘hierarchical’ => 1,
    ‘title_li’     => ”,
    ‘hide_empty’   => 0,
    ‘parent’  => $parentid
);
$number_of_series = count(get_terms(‘product_cat’,$args)); //count total number of terms related to passed taxonomy
$terms = get_terms(‘product_cat’, $term_args);
if ($terms) {
$sn=1;
$rn=1;
    foreach ($terms as $term) {
      $category_id = $term->term_id;
      if($sn == 1)
      $class = ‘first’;
      elseif($sn==3)
      $class = ‘last’;
      else
      $class = ”;
      ?>
      <li class=”product-category product <?php echo $class; ?>”>
      <a href=”<?php echo get_term_link($term->slug, ‘product_cat’); ?>”>
<?phpwoocommerce_subcategory_thumbnail( $term ); ?>
<h2 class=”woocommerce-loop-category__title”><?php echo $term->name ?></h2>
<div itemprop=”description”><?php echo $term->description; ?></div>
</a>
      </li>
  <?php
  if($rn%3==0)
  $sn=0;
  $rn++;
  $sn++;
  }  // end foreach
  // pagination begin
    $big = 999999999;
    echo ‘<div class=”row”><div class=”col-sm-12″>’;
    echo paginate_links( array(
     ‘base’ => str_replace( $big, ‘%#%’, esc_url( get_pagenum_link( $big ) ) ),
        ‘format’ => ‘?cat_paged=%#%’,
        ‘current’ => $page,
        ‘total’ =>  ceil($number_of_series / $per_page) // like 10 items per page
    ) );
    echo ‘</div></div>’;
/****************************/
}else{ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
/**
 * woocommerce_shop_loop hook.
 *
 * @hooked WC_Structured_Data::generate_product_data() – 10
 */
do_action( ‘woocommerce_shop_loop’ );
?>
<?php wc_get_template_part( ‘content’, ‘product’ ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php
/**
 * woocommerce_after_shop_loop hook.
 *
 * @hooked woocommerce_pagination – 10
 */
do_action( ‘woocommerce_after_shop_loop’ );
?>
<?php } ?>
<?php elseif ( ! woocommerce_product_subcategories( array( ‘before’ => woocommerce_product_loop_start( false ), ‘after’ => woocommerce_product_loop_end( false ) ) ) ) : ?>
<?php
/**
 * woocommerce_no_products_found hook.
 *
 * @hooked wc_no_products_found – 10
 */
do_action( ‘woocommerce_no_products_found’ ); ?>
<?php endif; ?>
<?php
/**
 * woocommerce_after_main_content hook.
 *
 * @hooked woocommerce_output_content_wrapper_end – 10 (outputs closing divs for the content)
 */
do_action( ‘woocommerce_after_main_content’ ); ?>
</div><!– product_cover –>
</div><!– container –>
<?php get_footer( ‘shop’ ); ?>
====================================

Leave a Reply

Your email address will not be published. Required fields are marked *


CAPTCHA Image
Reload Image