You are here:  » Custom New Search Filters


Custom New Search Filters

Submitted by stew on Wed, 2015-03-04 18:25 in

Hi David,

Hope all's well. Have looked around the forums for this but still unsure - if you're able to point in right direction that would be great.

I've been able to add new custom fields to the PT installation via:

http://www.pricetapestry.com/node/3094

The custom field I added was "Size" and have imported all product sizes.

Within the Wordpress installation I'm trying to display a filter for the sizes, so far I have the following code within the sidebar widget:

%IF_CATEGORIES%
Category:
%CATEGORIES%

%ENDIF_CATEGORIES%

%IF_BRANDS%
Brand:
%BRANDS%

%ENDIF_BRANDS%

%IFDB_size%
Size:
%DB_size%

%ENDIFDB_size%

Categories & brand are appearing fine - but size options don't appear, all I see it is the size shortcode text as above within the site,

Any ideas what I need to do here?

Many Thanks,

Stew

Submitted by support on Thu, 2015-03-05 07:47

Hi Stew,

All you need to do is duplicate the implementation code for one of the existing filters (I always use brand) to support your new size field, so if you edit pto_search.php and look for the following code at line 915 (version 2) or 955 (version 3)...

  $html = str_replace("%PTO_Q%",$pto_q,$html);

...and REPLACE with:

  $sql = "SELECT DISTINCT(size) FROM `".$pto_config_databaseTablePrefix."products` WHERE ".$pto_searchWhere." AND size <> '' ORDER BY size";
  if (($parts[0] != "size") && ($numRows = $wpdb->query($sql)))
  {
    $html_sizes = "";
    for($i=0;$i<$numRows;$i++)
    {
      $product = $wpdb->last_result[$i];
      $html_sizes .= "<option ".($pto_sizeFilter==$product->size?"selected='selected'":"")." value='".htmlentities($product->size,ENT_QUOTES,get_settings("blog_charset"))."'>".$product->size."</option>";
    }
    $html = str_replace("%SIZES%",$html_sizes,$html);
    $html = str_replace("%IF_SIZES%","",$html);
    $html = str_replace("%ENDIF_SIZES%","",$html);
  }
  else
  {
    $html = preg_replace('/%IF_SIZES%(.*)%ENDIF_SIZES%/','',$html);
  }
  $html = str_replace("%PTO_Q%",$pto_q,$html);

And the corresponding code in your Main / Search Filters Widget template, for consistency with the existing filters would be:

%IF_SIZES%
Size:
%SIZES%
%ENDIF_SIZES%

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Thu, 2015-03-05 15:14

Hi David,

Brilliant, many thanks, that's worked & sizes are now appearing.

The tick boxes aren't appearing though, and also if you take a look at:

{link saved}

The sizes are coming out in long rows, e.g:

34B,34C,34D,36B,36C,36D,38B,38C,40B

have you had any experience of this, am trying to achieve tick selection for Size similar to:

{link saved}

for example,

Will see what you think,

Many Thanks

Stew

Submitted by support on Thu, 2015-03-05 16:04

Hello Stew,

I just posted code for the exact same requirement for standalone Price Tapestry just the other day - check out http://www.pricetapestry.com/node/5584.

I'm not sure if you have added the necessary pto_search.php handling code for the new filter as this would need to be added also (at this time, you would probably find that selecting a size filter value has no effect), so assuming that's the case, changes are as follows:

In pto_search.php, look for the following code at line 60:

  $pto_brandFilter = (isset($pto_brandFilter)?$pto_brandFilter:"");

...and REPLACE with:

  $pto_brandFilter = (isset($pto_brandFilter)?$pto_brandFilter:"");
  global $pto_sizeFilter;
  $pto_sizeFilter = (isset($pto_sizeFilter)?$pto_sizeFilter:"");

Next look for the following code at line 92:

  if ($pto_brandFilter)
  {
    $priceWhere .= " AND brand = '".$wpdb->escape($pto_brandFilter)."' ";
  }

...and REPLACE with:

  if ($pto_brandFilter)
  {
    $priceWhere .= " AND brand = '".$wpdb->escape($pto_brandFilter)."' ";
  }
  if ($pto_sizeFilter)
  {
    $priceWhere .= " AND size LIKE '%".$wpdb->escape($pto_sizeFilter)."%' ";
  }

Next look for the following code at line 450:

  if ($pto_brandFilter) $sortBaseHREF .= "&pto_brandFilter=".urlencode($pto_brandFilter);

...and REPLACE with:

  if ($pto_brandFilter) $sortBaseHREF .= "&pto_brandFilter=".urlencode($pto_brandFilter);
  global $sizeFilter;
  if ($pto_sizeFilter) $sortBaseHREF .= "&pto_sizeFilter=".urlencode($pto_sizeFilter);

Next look for the following code at line 671:

  if ($pto_brandFilter) $navigationBaseHREF .= "&pto_brandFilter=".urlencode($pto_brandFilter);

...and REPLACE with:

  if ($pto_brandFilter) $navigationBaseHREF .= "&pto_brandFilter=".urlencode($pto_brandFilter);
  global $sizeFilter;
  if ($pto_sizeFilter) $navigationBaseHREF .= "&pto_sizeFilter=".urlencode($pto_sizeFilter);

Finally, as described previously you will have already added the handling code to display the size filter, but as you are currently seeing the values are the unique set of comma separated values used in the size field by your merchants. To extract() each comma separated value into a single list of unique sizes, where you have added the following code

    $html_sizes = "";
    for($i=0;$i<$numRows;$i++)
    {
      $product = $wpdb->last_result[$i];
      $html_sizes .= "<option ".($pto_sizeFilter==$product->size?"selected='selected'":"")." value='".htmlentities($product->size,ENT_QUOTES,get_settings("blog_charset"))."'>".$product->size."</option>";
    }

(shortly after line 915)

...REPLACE with:

    $rows1 = $wpdb->last_result;
    $newRows1 = array();
    foreach($rows1 as $row)
    {
      $sizes = explode(",",$row->size);
      foreach($sizes as $size)
      {
        $size = trim($size);
        if ($size)
        {
          $newRows1[$size]["size"] = $size;
        }
      }
    }
    ksort($newRows1);
    $rows1 = $newRows1;
    $numRows = count($rows1);
    $html_sizes = "";
    for($i=0;$i<$numRows;$i++)
    {
      $product = $rows1[$i];
      $html_sizes .= "<option ".($pto_sizeFilter==$product["size"]?"selected='selected'":"")." value='".htmlentities($product["size"],ENT_QUOTES,get_settings("blog_charset"))."'>".$product["size"]."</option>";
    }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Wed, 2015-12-09 18:53

Hi David,

Hope all's well, wondering if you can help. Have finally moved on with the site development and the sidebar search filters that we were looking at, an example is here:

{link saved}

This is a development site.

One query is at present the user needs to click "Apply" before the filters are applied.

If you look at a site like Asos.com the user is able to click a filter and the feed & filters update on-click.

{link saved}

I can't remember if the sidebar filters are a part of the PT plugin or if they are custom coded, thus am not sure if you can help here or are able to point in right direction of where I need to start?

Thanks,

Stew

Submitted by support on Tue, 2015-12-15 11:01

Hello Stew,

Sure - the sidebar filters were created in a modification to your pto_search.php. To enable auto-submit on selection (click) of a filter value, edit that file and search for each instance of:

type='checkbox'

...(lines 799/818/837/873/909) and REPLACE with:

type='checkbox' onchange='JavaScript:this.form.submit();'

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stew on Thu, 2015-12-17 10:41

Hi David,

Brilliant many thanks for that, will try the code out,

Many thanks!

Cheers

Stewart