You are here:  » Filter sizes


Filter sizes

Submitted by henk on Thu, 2012-12-20 19:44 in

Hi David,

I use sizes as filter, but sometimes the sizes comes like:

39,40,41,43,45

and the next product:

38,40,41,43,46

Is it possible to make this in one row? like:

38
39
40
41
43
45
46

Thx
Henk

Submitted by support on Fri, 2012-12-21 11:00

Hi Henk,

Sure - first you'd need to change the $priceWhere construction so that the size filter is applied using LIKE rather than EQUALS. To do this, in your modified pto_product.php where you currently have at line 164:

if ($$fn) $priceWhere .= " AND ".$pto_filter." = '".$wpdb->escape($$fn)."' ";

...REPLACE this with:

if ($pto_filter=="size")
{
  if ($$fn) $priceWhere .= " AND size LIKE '%".$wpdb->escape($$fn)."%' ";
}
else
{
  if ($$fn) $priceWhere .= " AND ".$pto_filter." = '".$wpdb->escape($$fn)."' ";
}

Since you're using "generic" code to create each filter, the easiest way to "rewrite" the options array is to re-create the $wpdb->last_result array. To do this, look for the following code at line 1018:

  for($i=0;$i<$numRows;$i++)

...and REPLACE with:

  if ($pto_filter == "size")
  {
    $allSizes = array();
    for($i=0;$i<$numRows;$i++)
    {
      $sizes = explode(",",$wpdb->last_result[$i]->size);
      foreach($sizes as $size) $allSizes[$size] = 1;
    }
    ksort($allSizes);
    $wpdb->last_result = array();
    foreach($allSizes as $size => $v)
    {
      $wpdb->last_result[] = (object)array("size"=>$size);
    }
    $numRows = count($wpdb->last_result);
  }
  for($i=0;$i<$numRows;$i++)

...should be close!

Cheers,
David.
--
PriceTapestry.com

Submitted by henk on Fri, 2012-12-21 19:44

Hi David,

I think this I must changing this in pto_search.php, but nothing changed my filter:

maat=size

this one gives an idea:

{link saved}

Thx
Henk

Submitted by support on Sat, 2012-12-22 08:51

Hi Henk,

That should do it - e.g.

  if ($pto_filter == "maat")
  {
    $allSizes = array();
    for($i=0;$i<$numRows;$i++)
    {
      $sizes = explode(",",$wpdb->last_result[$i]->maat);
      foreach($sizes as $size) $allSizes[$size] = 1;
    }
    ksort($allSizes);
    $wpdb->last_result = array();
    foreach($allSizes as $size => $v)
    {
      $wpdb->last_result[] = (object)array("maat"=>$size);
    }
    $numRows = count($wpdb->last_result);
  }
  for($i=0;$i<$numRows;$i++)

Cheers,
David.
--
PriceTapestry.com

Submitted by henk on Sat, 2012-12-22 11:37

Looks good!!!!

There are some double rows;

33
33
34
35
36
37
37

And is it possible to make it numeric alphabetical.

Thx
Henk

Submitted by support on Sun, 2012-12-23 10:09

Hello Henk,

That's most likely a trim() required on the values (some having spaces, others not). Using SORT_STRING should give numeric first, alphanumeric second... Have a go with:

  if ($pto_filter == "maat")
  {
    $allSizes = array();
    for($i=0;$i<$numRows;$i++)
    {
      $sizes = explode(",",$wpdb->last_result[$i]->maat);
      foreach($sizes as $size) $allSizes[trim($size)] = 1;
    }
    ksort($allSizes,SORT_STRING);
    $wpdb->last_result = array();
    foreach($allSizes as $size => $v)
    {
      $wpdb->last_result[] = (object)array("maat"=>$size);
    }
    $numRows = count($wpdb->last_result);
  }
  for($i=0;$i<$numRows;$i++)

Cheers,
David.
--
PriceTapestry.com

Submitted by henk on Sun, 2012-12-23 10:36

Thx David.

Henk