Fix for WP e-Commerce bug, pagination links in tag and search result pages don’t work.

Posted by on Aug 25, 2012 in Blog, WP e-Commerce | 5 Comments

The question from a Storefront user

Recently, the Product Cloud Tag Widget was activated. When one of the tags is click on, the first page loads correctly. However, when the ‘next’, ‘last’ or a page number is clicked in the pagination, the displayed items changed from tagged to all products (product-page).

Answer

This is a bug. A pretty bad one really, seems to have been around a long time too. It can be fixed with a small PHP edit in the WP e-Commerce plugin. If you have never edited PHP see the notes and tools at the end of this post. This post is written for the 3.8.8.5 version of WP e-Commerce. This may be fixed one day. If not I imagine the code will be much the same. The line numbers may change, so I will post a good chunk of code to help you identify the right piece to modify. Credit to Dean at ChainsawDR for most of this fix. I have added some code so that you do not need to hardcode the URL for your site and changed the search location to the more standard WordPress ?s=. As noted in the code you may need to change that if you have special search page or a plugin that uses a different URL. You can check this easily by doing a search and checking the URL in the browser.

Code

Open wp-content/plugins/wp-e-commerce/wpsc-includes/product-template.php
Find the highlighted code which should be at line 116 if you have WP e-Commerce 3.8.8.5. Do not be fooled by the identical line at 105 as I was the first time I did this.

	//if there is no pagination
	if(!get_option('permalink_structure')) {
		$category = '?';
		if(isset($wp_query->query_vars['wpsc_product_category']))
			$category = '?wpsc_product_category='.$wp_query->query_vars['wpsc_product_category'];
		if(isset($wp_query->query_vars['wpsc_product_category']) && is_string($wp_query->query_vars['wpsc_product_category'])){

			$page_link = get_option('blogurl').$category.'&paged';
		}else{
			$page_link = get_option('product_list_url').$category.'&paged';
		}

		$separator = '=';
	}else{
		if ( isset( $wp_query->query_vars['wpsc_product_category'] ) ) {
			$category_id = get_term_by( 'slug', $wp_query->query_vars['wpsc_product_category'], 'wpsc_product_category' );
			$page_link = trailingslashit( get_term_link( $category_id, 'wpsc_product_category' ) );
		} else {
			$page_link = trailingslashit( get_option( 'product_list_url' ) );
		}
		$separator = 'page/';
	}

	// If there's only one page, return now and don't bother
	if($totalpages == 1)
		return;
	// Pagination Prefix
	$output = __('Pages: ','wpsc');

Replace the highlighted lines with all of the code below.
If you do need to change the search you just need to change the part that comes after your sites URL in the highlighted line in the new code, which is also commented. In my case the URL of a search result page for fruit was http://mysite.com/?s=fruit. So I added ?s= in that line. You put whatever comes between the URL .com/ and the search term in place of ?s= below.

if(isset($wp_query->query_vars['wpsc_product_category']))
$page_link = trailingslashit(get_option('product_list_url')).$wp_query->query_vars['wpsc_product_category'].'/';
elseif(isset($wp_query->query_vars['product_tag']))
$page_link = trailingslashit(get_bloginfo('url')).'tagged/'.$wp_query->query_vars['product_tag'].'/';
else //if its not a product category and its not a product tag then presuming it must be a product search, if this messes up for anything else then add in another if statement for that scenario
$page_link = trailingslashit(get_bloginfo('url')).'?s='.($_GET["product_search"]).'&'; // your search URL maybe different, change accordingly

if(isset($wp_query->query_vars['wpsc_product_category']))
$separator = 'page/';
elseif(isset($wp_query->query_vars['product_tag']))
$separator = 'page/';
else //if its not a product category and its not a product tag then presuming it must be a product search, if this messes up for anything else then add in another if statement for that scenario
$separator = 'page=';
}

Tools:

You may edit your PHP outside WordPress and upload it to your server. You must use an appropriate code editor NOT microsoft word or anything like that which adds a lot of invisible formatting and will be a disaster.
If you do not have one, 2 excellent free ones are…
TextWrangler for Mac, http://www.barebones.com/products/textwrangler/
Notepad++ for the PC http://notepad-plus-plus.org/

If you want to go all out Aptana, this is a serious and powerful code editor for both mac and pc. It is free. Has handy code hinting, that is as you type it shows you what the options are for the setting you are working with. It could be overkill, or great. http://aptana.com/

5 Comments

  1. WPSC Pagination not working in search results - WordPress BuddyPress Tweaks
    June 17, 2014

    […] tried this fix but no luck and I don’t believe this was the root of the problem. Can anyone shed some light […]

    Reply
  2. WPSC Pagination not working in search results
    June 17, 2014

    […] tried this fix but no luck and I don’t believe this was the root of the problem. Can anyone shed some light […]

    Reply
  3. Anthony K.
    January 12, 2013

    To search for multiple word text like “nike trainer” replace line 121 in the code above with the following lines:

    // You can replace ‘product_search’ with the name of the input text in searchform.php… most probably ‘s’

    $get_search_query = $_GET[‘product_search’];
    $get_search_query = str_replace(” “, “+”, $get_search_query);

    $page_link = trailingslashit(get_bloginfo(‘url’)).’?s=’.$get_search_query.’&’;

    =================
    Other notes:
    1.
    If your pagination does not work, try replacing line 128 with:
    $separator = ‘paged=’;

    ====
    2.
    If you are using Version 3.8.9.4 of WP e-Commerce, the following complete code can be used. I have copied the whole edited code from your original line 107 to line 153. You should follow and match through with your code before replacing. Remember this is for WP e-Commerce Version 3.8.9.4 and no other version.

    //if there is no pagination
    if(!get_option(‘permalink_structure’)) {
    $category = ‘?’;

    if( ! empty( $wp_query->query_vars[‘wpsc_product_category’] ) )
    $category = ‘?wpsc_product_category=’.$wp_query->query_vars[‘wpsc_product_category’];
    if(isset($wp_query->query_vars[‘wpsc_product_category’]) && is_string($wp_query->query_vars[‘wpsc_product_category’])){
    $page_link = get_option(‘blogurl’).$category.’&paged’;
    }else{
    $page_link = get_option(‘product_list_url’).$category.’&paged’;
    }

    $separator = ‘=’;
    }else{
    global $wpsc_query;

    $separator = ‘page/’;

    if ( isset( $wp_query->query_vars[‘wpsc_product_category’] ) ) {
    $category_id = get_term_by( ‘slug’, $wp_query->query_vars[‘wpsc_product_category’], ‘wpsc_product_category’ );

    /*
    Anto: Fix for pagination when searching. One line fix here, the rest in the else statement
    Fix from: http://gasolicious.com/commerce-pagination-links-search/

    $page_link = trailingslashit( get_term_link( $category_id, ‘wpsc_product_category’ ) );
    */
    $page_link = trailingslashit(get_option(‘product_list_url’)).$wp_query->query_vars[‘wpsc_product_category’].’/’;

    // in case we’re displaying a category using shortcode, need to use the page’s URL instead of the taxonomy URL
    if ( $wp_the_query->is_page() ) {
    $page = $wp_the_query->get_queried_object();
    if ( preg_match( ‘/\[wpsc\_products[^\]]*category_id=/’, $page->post_content ) ) {
    $page_link = trailingslashit( get_permalink( $page->ID ) );
    $separator = ”;
    }
    }
    } elseif ( is_tax( ‘product_tag’ ) ) {
    $tag = get_queried_object();
    $page_link = trailingslashit( get_term_link( (int) $tag->term_id, ‘product_tag’ ) );
    } elseif ( $wpsc_query->is_front_page() ) {
    $page_link = trailingslashit( home_url() );
    } else
    {
    /*
    Anto: Fix for pagination when searching
    Fix from: http://gasolicious.com/commerce-pagination-links-search/
    $page_link = trailingslashit( get_option( ‘product_list_url’ ) );
    $separator = ”;
    */

    $get_search_query = $_GET[‘s’];
    $get_search_query = str_replace(” “, “+”, $get_search_query);

    $page_link = trailingslashit(get_bloginfo(‘url’)).’?s=’.$get_search_query.’&’; // your search URL maybe different, change accordingly

    if(isset($wp_query->query_vars[‘wpsc_product_category’]))
    {
    $separator = ‘page/’;
    }
    elseif(isset($wp_query->query_vars[‘product_tag’]))
    {
    $separator = ‘page/’;
    }
    /*if its not a product category and its not a product tag then presuming it must be a product search,
    if this messes up for anything else then add in another if statement for that scenario*/
    else
    {
    $separator = ‘page=’;
    }
    }

    if ( version_compare( get_bloginfo( ‘version’ ), ‘3.4’, ‘<' ) )
    $separator = 'page/';
    }

    // If there's only one page, return now and don't bother
    if($totalpages == 1)
    return;
    // Pagination Prefix

    Reply
  4. Ben
    November 4, 2012

    Hi i am using your code snippet here. It works great the only problem ive got is that if a user searches something like “nike trainer” the space is replaced by a “+” in the url and becomes “nike+trainer”. However as soon as you use the pagination the “+” dissapears and becomes “niketrainer” which returns no results.

    Thank you for your help

    Reply
  5. Thomas
    October 1, 2012

    Nice code. Used it on my website (airlinejunk.com) and it seems to solve the problem BUT when searching for multiple words (example: Lufthansa 747) and then clicking on page2/next page, the url merges both search strings… Lufthansa%20747 —> becomes Lufthansa747 and thus there are zero results on the 2nd page. Any ideas how to fix this? Am I doing something wrong?

    I’d appreciate your feedback/help.
    T.

    Reply

Leave a Reply