You are here:  » Extra filters


Extra filters

Submitted by henk on Fri, 2012-11-02 22:20 in

Hi David,

If i want extra filters where do i have to start?

Thx
Henk

Submitted by support on Sat, 2012-11-03 12:59

Hello Henk,

One thing to do would be to create generic code, from which you can add filters simply by populating an array with the name of each custom field you wish to filter.

Firstly, look for the following code at line 308 of pto.php

  array_push($vars, 'pto_brandFilter');

...and REPLACE with:

  array_push($vars, 'pto_brandFilter');
  global $pto_filters;
  $pto_filters = array("custom1","custom2");
  foreach($pto_filters as $pto_filter)
  {
    array_push($vars, 'pto_".$pto_filter."Filter');
  }

Replace custom1, custom2 with your desired filter field names (exactly as they appear in your pt_products table), adding additional entries if required.

Then in pto_search.php look for the following code at line 156:

    $html = "";

...and REPLACE with:

  global $pto_filters;
  foreach($pto_filters as $pto_filter)
  {
    $fn = "pto_".$pto_filter."Filter";
    $$fn = $_GET[$fn];
    if ($$fn) $priceWhere .= " AND ".$pto_filter." = '".$wpdb->escape($$fn)."' ";
  }
  $html = "";

Next look for the following code at line 387:

  $sortBaseHREF .= "&pto_sort=";

...and REPLACE with:

  global $pto_filters;
  foreach($pto_filters as $pto_filter)
  {
    $vn = "pto_".$pto_filter."Filter";
    $$vn = (isset($_GET[$vn])?$_GET[$vn]:"");
    if ($$vn) $sortBaseHREF .= "&".$vn."=".urlencode($$vn);
  }
  $sortBaseHREF .= "&pto_sort=";

Next look for the following code at line 604:

      $navigationBaseHREF .= "&pto_page=";

...and REPLACE with:

      global $pto_filters;
      foreach($pto_filters as $pto_filter)
      {
        $vn = "pto_".$pto_filter."Filter";
        $$vn = (isset($_GET[$vn])?$_GET[$vn]:"");
        if ($$vn) $navigationBaseHREF .= "&".$vn."=".urlencode($$vn);
      }
      $navigationBaseHREF .= "&pto_page=";

And finally look for the following code at line 915:

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

...and REPLACE with:

    global $pto_filters;
    foreach($pto_filters as $pto_filter)
    {
      $vn = "pto_".$pto_filter."Filter";
      $sql = "SELECT DISTINCT(".$pto_filter.") FROM `".$pto_config_databaseTablePrefix."products` WHERE ".$pto_searchWhere." AND ".$pto_filter." <> '' ORDER BY ".$pto_filter;
      if ($numRows = $wpdb->query($sql))
      {
        $html_filter = ucwords($pto_filter)."<br />";
        $html_filter .= "<select name='pto_".$pto_filter."Filter'><option value=''>All</option>";
        for($i=0;$i<$numRows;$i++)
        {
          $product = $wpdb->last_result[$i];
          $html_filter .= "<option ".($$vn==$product->$pto_filter?"selected='selected'":"")." value='".htmlentities($product->$pto_filter,ENT_QUOTES,get_settings("blog_charset"))."'>".$product->$pto_filter."</option>";
        }
        $html_filter .= "</select><br />";
      }
      $html_filters .= $html_filter;
    }
    $html = str_replace("%FILTERS%",$html_filters,$html);
    $html = str_replace("%PTO_Q%",$pto_q,$html);

With the above in place, edit your Main / Search Filters Widget template (wp-admin > Settings > PriceTapestry.org) and insert the placeholder

%FILTERS%

...at the point at which you want your custom filters to display.

The title for each custom filter is generated as the Name Case of the field name, so if you have "custom1" in your $pto_filters array, the title on the filters widget will be "Custom1". This can be easily modified to be different to the field name if required.

Cheers,
David.
--
PriceTapestry.com