• 0
Sign in to follow this  
Followers 0

Pagination not working in a Custom loop


Question

Posted · Report post

I wrote a custom section to conduct a multi-taxonomy search for a client. The search functionality works perfectly but the pagination in the section doesnt - the loop shows the same results on every page. Any feedback is welcome

 

The code for the section.php is posted here:

<?php
/*
	Section: Tyrebroker Custom Product Search
	Author: Amit Wadhwa
	Author URI: http://xava.ie
	Description: A base section to be used to start new sections.
	Class Name: tbCustomSearch
	Demo:
	Version: 1.2
	Filter: component
*/

/**
 * DMS Meta Info Tips

	// You can set the filter value to one of the following: component, layout, full-width, format, gallery , nav, slider, social, widgetized, misc
	// There are a few additional headers you can utlize. One of which is a full-width filter that forces the section full-width. The other is an active loading filter, which, when applied, doesn't require a page refresh for the section to show up. however, sections with javascript shouldn't use this.
*/



class tbCustomSearch extends PageLinesSection {

	const version = '1.2';  // Declares a version, used in tracking the version of the script. For some reason it's needed with DMS along with declaring true at the end of the script to force into the footer

    // READY TO USE VARIABLES
    // $this->id;          section slug, in this case its nb-section
    // $this->base_url;    the base url of the section
    // $this->base_dir;    the base directory of the section
    // $this->images;      create an /images directory in your section, then use this variable for the path
    // $this->screenshot;  the section thumb
    // $this->splash;      the section splash
    // $this->description  get the description from the header
    // get_the_id();       retrieves the clone id of the section

    // RUNS ALL TIME - This loads all the time, even if the section isn't on the page. Stuff like actions and things can go here, as well as post type setup functions
    function section_persistent(){}

    // LOAD SCRIPTS
    function section_scripts(){
        //wp_enqueue_script('script-name', $this->base_url.'/script.js', array('jquery'), self::version, true );
    }

    // RUNS IN <HEAD>
    function section_head() {

    	// Always use jQuery and never $ to avoid issues with other store products

        /*
    	?><script>
	    	jQuery(document).ready(function(){

	    	});
    	</script><?php
    	*/

        // This is only needed if you'll be using custom fonts
        // echo load_custom_font($this->opt('nb_custom_font'), '.target-class');

    }

    // BEFORE SECTION - this adds a class to the section wrap. you can also put HTML here and it will run outside of the section, and before it
    function before_section_template( $location = '', $clone_id = null ) {

		//$this->wrapper_classes['background'] = 'special-class';

	}

    // SECTION MARKUP - This is the function that outputs all the HTML onto the page. Put all your viewable content here
   	function section_template() {
   		//global $product, $post;
   		global $my_products;
		$width = $_GET['width'];
		$aratio = $_GET['aratio'];
		$dia = $_GET['dia'];
		$paged=$_GET['paged']; if (!$paged){$paged = 1;}
		?>
		<div class="bordered" style="margin-top:20px;">
			<div class="row">
				<span class="span12">
					<span style="font-size: 20px; font-weight: bold;display:inline; margin-bottom: 0px; margin-top: 0px;">Search our stock for the cheapest fitted tyres in Dublin</span>
				</span>
			</div>
			<div class="row" style="margin-bottom: 0px;">
				<div class="span12">	
					<form id="tyresearch" name="search" method="GET" action="http://www.tyrebroker.ie/your-search" style="margin: 0px"> 
						<fieldset style="width:25%;display:inline">
							<select name="width" style="width: 100%;">
								<option value="0">Tyre Width</option><option value="135"<?php if ($width == 135) echo " selected='selected'"; ?>>135</option><option value="145"<?php if ($width == 145) echo " selected='selected'"; ?>>145</option><option value="155"<?php if ($width == 155) echo " selected='selected'"; ?>>155</option><option value="165"<?php if ($width == 165) echo " selected='selected'"; ?>>165</option><option value="175"<?php if ($width == 175) echo " selected='selected'"; ?>>175</option><option value="185"<?php if ($width == 185) echo " selected='selected'"; ?>>185</option><option value="195"<?php if ($width == 195) echo " selected='selected'"; ?>>195</option><option value="205"<?php if ($width == 205) echo " selected='selected'"; ?>>205</option><option value="215"<?php if ($width == 215) echo " selected='selected'"; ?>>215</option><option value="225"<?php if ($width == 225) echo " selected='selected'"; ?>>225</option><option value="235"<?php if ($width == 235) echo " selected='selected'"; ?>>235</option><option value="245"<?php if ($width == 245) echo " selected='selected'"; ?>>245</option><option value="255"<?php if ($width == 255) echo " selected='selected'"; ?>>255</option><option value="265"<?php if ($width == 265) echo " selected='selected'"; ?>>265</option><option value="275"<?php if ($width == 275) echo " selected='selected'"; ?>>275</option><option value="285"<?php if ($width == 285) echo " selected='selected'"; ?>>285</option><option value="295"<?php if ($width == 295) echo " selected='selected'"; ?>>295</option><option value="305"<?php if ($width == 305) echo " selected='selected'"; ?>>305</option><option value="315"<?php if ($width == 315) echo " selected='selected'"; ?>>315</option><option value="325"<?php if ($width == 325) echo " selected='selected'"; ?>>325</option>
							</select>
						</fieldset >
						<fieldset style="width:25%;display:inline">
							<select name="aratio" style="width:100%">
								<option value="0">Tyre Profile</option><option value="25"<?php if ($aratio == 25) echo " selected='selected'"; ?>>25</option><option value="30"<?php if ($aratio == 30) echo " selected='selected'"; ?>>30</option><option value="35"<?php if ($aratio == 35) echo " selected='selected'"; ?>>35</option><option value="40"<?php if ($aratio == 40) echo " selected='selected'"; ?>>40</option><option value="45"<?php if ($aratio == 45) echo " selected='selected'"; ?>>45</option><option value="50"<?php if ($aratio == 50) echo " selected='selected'"; ?>>50</option><option value="55"<?php if ($aratio == 55) echo " selected='selected'"; ?>>55</option><option value="60"<?php if ($aratio == 60) echo " selected='selected'"; ?>>60</option><option value="65"<?php if ($aratio == 65) echo " selected='selected'"; ?>>65</option><option value="70"<?php if ($aratio == 70) echo " selected='selected'"; ?>>70</option><option value="75"<?php if ($aratio == 75) echo " selected='selected'"; ?>>75</option><option value="80"<?php if ($aratio == 80) echo " selected='selected'"; ?>>80</option><option value="85"<?php if ($aratio == 85) echo " selected='selected'"; ?>>85</option>
							</select>
						</fieldset >
						<fieldset style="width:25%;display:inline">
							<select name="dia" style="width:100%">
								<option value="0">Tyre Size</option><option value="8"<?php if ($dia == 8) echo " selected='selected'"; ?>>8</option><option value="10"<?php if ($dia == 10) echo " selected='selected'"; ?>>10</option><option value="12"<?php if ($dia == 12) echo " selected='selected'"; ?>>12</option><option value="13"<?php if ($dia == 13) echo " selected='selected'"; ?>>13</option><option value="14"<?php if ($dia == 14) echo " selected='selected'"; ?>>14</option><option value="15"<?php if ($dia == 15) echo " selected='selected'"; ?>>15</option><option value="16"<?php if ($dia == 16) echo " selected='selected'"; ?>>16</option><option value="17"<?php if ($dia == 17) echo " selected='selected'"; ?>>17</option><option value="18"<?php if ($dia == 18) echo " selected='selected'"; ?>>18</option><option value="19"<?php if ($dia == 19) echo " selected='selected'"; ?>>19</option><option value="20"<?php if ($dia == 20) echo " selected='selected'"; ?>>20</option><option value="21"<?php if ($dia == 21) echo " selected='selected'"; ?>>21</option><option value="22"<?php if ($dia == 22) echo " selected='selected'"; ?>>22</option>
							</select>
						</fieldset >
						<fieldset style="width:15%;display:inline">
							<input type="hidden" name="paged" value="1">
							<input type="submit" value="Find Tyres"  style="width:100%; vertical-align:bottom"/>
						</fieldset >
					</form>
				</div>	
			</div>
			<div class="clear"></div>
		</div>
		<?php
		if ( ($_GET['width'] == 0) && ($_GET['aratio'] == 0) && ($_GET['dia'] == 0) ) { 
			echo 'Please select a tyre size to search from or take a look at our most popular tyres.';
		} else { if ($width && !$aratio && !$dia){
			$aratiop = "--";
			$diap = "--";
			$my_products = new WP_Query(
				array(
					'paged' => $paged,
					'post_type' => 'product',
					'tax_query' => array(
						'relation' => 'AND',
						array(
							'taxonomy' => 'pa_width',
							'field' => 'slug',
							'terms' => $width)
					),
					'orderby' => 'meta_value_num',
					'meta_key' => '_regular_price',
					'order' => 'ASC'
				)
			);
		} else if ($width && $aratio && !$dia){
			$aratiop = $aratio;
			$diap = "--";
			$my_products = new WP_Query(
				array(
					'paged' => $paged,
					'post_type' => 'product',
					'tax_query' => array(
						'relation' => 'AND',
						array(
							'taxonomy' => 'pa_width',
							'field' => 'slug',
							'terms' => $width),
						array(
							'taxonomy' => 'pa_aratio',
							'field' => 'slug',
							'terms' => $aratio)
					),
					'orderby' => 'meta_value_num',
					'meta_key' => '_regular_price',
					'order' => 'ASC'
				)
			);
		}else if ($width && $aratio && $dia){
			$aratiop = $aratio;
			$diap = $dia;
			$my_products = new WP_Query(
				array(
					'orderby' => 'meta_value_num',
					'meta_key' => '_regular_price',
					'order' => 'ASC',
					'paged' => $paged,
					'post_type' => 'product',
					'tax_query' => array(
						'relation' => 'AND',
						array(
							'taxonomy' => 'pa_width',
							'field' => 'slug',
							'terms' => $width),
						array(
							'taxonomy' => 'pa_aratio',
							'field' => 'slug',
							'terms' => $aratio),
						array(
							'taxonomy' => 'pa_dia',
							'field' => 'slug',
							'terms' => $dia)
					)
				)
			);
		}
			if (!($my_products->have_posts())){ ?>
				<div class="notfound">
					Your search for <?php echo $width;?>/<?php echo $aratiop; ?>R<?php echo $diap; ?> yielded no results<br>
					Please contact us for prices.
				</div>
			<?php
			}else{ 
				$i=0;
				$numposts = $my_products->have_posts() ? sizeof($my_products->posts) : 0; 
				$num_pages = $my_products->max_num_pages;
				// * sizeof($my_products->posts); ?>
				<div class="restitle" style="float:left"> Search Results for tyre sizes <?php echo $width; ?>/<?php echo $aratiop; ?>R<?php echo $diap; ?> </div><div style="float:right; display:inline">Page <?php echo $i+1; ?> of <?php echo $num_pages;  ?></div>. </div>
				<div class="clear"></div>
				<p>
				<div id = "search_heading" class="row" style="border:1px solid rgba(102,155, 204, 0.5);padding:10px  0px;">
					<div class="span2" style="margin: 0px; padding: 5px"><strong>Brand</strong></div>
					<div class="span5" style="margin: 0px; padding: 5px"><strong>Product Details</strong></div>
					<div class="span1" style="margin: 0px; padding: 5px"><strong>LR/SR<sup><a href="#notes">*</a></sup></strong></div>
					<div class="span1" style="margin: 0px; padding: 5px"><strong>EU<sup><a href="#notes">**</a></sup></strong></div>
					<div class="span1" style="margin: 0px; padding: 5px"><strong>Price</strong></div>
					<div class="span2" style="margin: 0px; padding: 5px"><strong> </strong></div>
				</div>
						
				<?php while ($my_products->have_posts()) : $my_products->the_post(); global $product;					
					// get the terms related to post
					$brands = get_the_terms( $post->ID, 'pa_brand' );
					foreach( $brands as $tyreBrand ) {$tyrebrand = $tyreBrand->name;} 
					$widths = get_the_terms( $post->ID, 'pa_width' );
					foreach( $widths as $tyreWidth ) {$tyrewidth = $tyreWidth->name;} 
					$aratios = get_the_terms( $post->ID, 'pa_aratio' );
					foreach( $aratios as $tyreAratio ) {$tyrearatio = $tyreAratio->name;} 
					$dias = get_the_terms( $post->ID, 'pa_dia' );
					foreach( $dias as $tyreDia ) {$tyredia = $tyreDia->name;}
					$euratings = get_the_terms( $post->ID, 'pa_eurating' );
					if ($euratings){
						foreach( $euratings as $tyreEU ){$tyreeu = $tyreEU->name;}
					}else{
						$tyreeu = 'Not Supplied';
					}
					$lspeds = get_the_terms( $post->ID, 'pa_lsped' );
					foreach( $lspeds as $tyreLS ) {$tyrels = $tyreLS->name;} 
					?>
								<div id = "post-<?php the_ID(); ?>" <?php post_class(); ?> >				
						<div class="prow-<?php echo $i; ?> row <?php if($i%2 == 0){echo "row-grey";} ?>" style="margin-bottom:0px;border-bottom:1px solid rgba(102,155, 204, 0.5);border-right:1px solid rgba(102,155, 204, 0.5);border-left:1px solid rgba(102,155, 204, 0.5);">
							<div class="span2" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo $tyrebrand; ?></span></a></div>
							<div class="span5" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo the_title(); ?></span></a></div>
							<div class="span1" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo $tyrels; ?></span></a></div>
							<div class="span1" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo $tyreeu; ?></span></a></div>
							<div class="span1" style="margin: 0px; padding: 5px">
<span class="price"><?php echo $product->get_price_html(); ?></span></div>
							<div class="span2" style="margin: 0px; padding: 5px">
								<div class="product-avlb">
									<?php if($product->is_in_stock() ) { 
										$content = "[add_to_cart id=\"".$post->ID."\"]";?>
										<?php echo do_shortcode( $content ); ?>
										<form class="cart" method="post" enctype='multipart/form-data'>
	 										<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
	 										<?php
									 		if ( ! $product->is_sold_individually() )
									 			woocommerce_quantity_input( array(
									 				'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
									 				'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )	) );
	 										?>
	 										<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
	 										<button type="submit" class="single_add_to_cart_button button-alt"><?php echo $product->single_add_to_cart_text(); ?></button>
	 										<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
										</form>
	
									<?php }
									else { ?>
										<div style="align:right"><a id="not-in-stock-link" href="http://www.tyrebroker.ie/contact-us" class="button">Contact Us</a></div>
									<?php }?>
								</div>
							</div>					
						</div>
					</div>
					<div class="clear"></div>	        
					<?php $i++;
				endwhile;
			}
			wp_reset_query();
			if($my_products->max_num_pages>1){ ?>
				<nav class="woocommerce-pagination pager">
					<?php
						echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
							'base' 			=> str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ),
							'format' 		=> '',
							'current' 		=> max( 1, $paged),
							'total' 		=> $my_products->max_num_pages,
							'prev_text' 	=> '&larr;',
							'next_text' 	=> '&rarr;',
							'type'			=> 'list',
							'end_size'		=> 3,
							'mid_size'		=> 3
						) ) );
					?>
				</nav>
			<?php } ?>
			<h3 class="headline">Can't find what you need, <a href="http://www.tyrebroker.ie/contact-us">contact us</a> for a quote.</h3>
			<div class="footnote">All tyrebroker.ie quotes are valid till the end of business on the date of the quote or until stocks last.</div><p></p>		
		<?php } 
        // call settings like so
        // $var = $this->opt($this->id.'_some_key');


        // print iterated options - as of 1.1

   	}

    // RUNS IN <FOOTER> - This is just like using wp_footer so this stuffs will in the footer of your site
	function section_foot(){}


    // WELCOME MESSAGE - HTML content for the welcome/intro option field
	function welcome(){

		ob_start();

		?><div style="font-size:12px;line-height:14px;color:#444;"><p><?php _e('You can have some custom text here.','nb-section');?></p></div><?php

		return ob_get_clean();
	}

    // SECTION OPTIONS - draws out the section options. This symbol * denotes optional fields.

} // that's it, that's the end of it. never put code past this area as it's then out of the class

Share this post


Link to post
Share on other sites

1 answer to this question

  • 0

Posted · Report post

Hi there, the moderator team aren't developers so we're not the best people to advise on the above unfortunately as it's outside of our immediate support spectrum.

 

Are you a member of the Developer Community/300? If you are ask in their skype room and they might be able to advise further for you. http://www.pagelines.com/the-300/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0