You are here:  » More Custom Fields In Product Mapping


More Custom Fields In Product Mapping

Submitted by fotisman01 on Sat, 2015-02-21 08:50 in

Good Morning David.

I was wondering if it's possible to add more custom fields in product mapping ( Joomla installation and pricetapestry 14/06A)

I love product mapping and I would like to add more 2 more custom fields when mapping a product, and then display them in product filters when searching! Can you help me out?

Now I have the fields

Custom Category:
Custom Brand:

I would like to ad 2 more fields

Custom Age: (with just text just like category and brand above)
Custom Weight: (with just text just like category and brand above)

I would like these fields to be displayed in the Search Filters Widget (Joomla).

Can you please help me???

Thank you David!!!!

Submitted by support on Sat, 2015-02-21 09:40

Hi,

Sure - this can be done simply by replicating the code for category / brand as required. First, add the two new custom fields to the `productsmap` table with the following dbmod.php script:

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."productsmap`
            ADD `age` VARCHAR(255) NOT NULL,
            ADD `weight` VARCHAR(255) NOT NULL
          "
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Next, edit admin/productsmap_configure.php and look for the following code at line 72:

  image_url = '".database_safe(widget_posted($_POST["image_url"]))."'

...and REPLACE with:

  image_url = '".database_safe(widget_posted($_POST["image_url"]))."',
  age = '".database_safe(widget_posted($_POST["age"]))."',
  weight = '".database_safe(widget_posted($_POST["weight"]))."'

And then the following code at line 103:

  widget_textBox("Custom Image URL","image_url",FALSE,$productmap["image_url"],"",6);

...and REPLACE with:

  widget_textBox("Custom Image URL","image_url",FALSE,$productmap["image_url"],"",6);
  widget_textBox("Custom Age","age",FALSE,$productmap["age"],"",6);
  widget_textBox("Custom Weight","weight",FALSE,$productmap["weight"],"",6);

Then in includes/admin.php look for the following code at line 317:

  if ($admin_importProductMappingsOverrides[$importRecord["name"]]["image_url"]) $importRecord["image_url"] = $admin_importProductMappingsOverrides[$importRecord["name"]]["image_url"];

...and REPLACE with:

  if ($admin_importProductMappingsOverrides[$importRecord["name"]]["image_url"]) $importRecord["image_url"] = $admin_importProductMappingsOverrides[$importRecord["name"]]["image_url"];
  if ($admin_importProductMappingsOverrides[$importRecord["name"]]["age"]) $importRecord["age"] = $admin_importProductMappingsOverrides[$importRecord["name"]]["age"];
  if ($admin_importProductMappingsOverrides[$importRecord["name"]]["weight"]) $importRecord["weight"] = $admin_importProductMappingsOverrides[$importRecord["name"]]["weight"];

And final the following code at line 477:

  $admin_importProductMappingsOverrides[$productsmap["name"]]["image_url"] = (($productsmap["image_url"])?$productsmap["image_url"]:"");

...and REPLACE with:

  $admin_importProductMappingsOverrides[$productsmap["name"]]["image_url"] = (($productsmap["image_url"])?$productsmap["image_url"]:"");
  $admin_importProductMappingsOverrides[$productsmap["name"]]["age"] = (($productsmap["age"])?$productsmap["age"]:"");
  $admin_importProductMappingsOverrides[$productsmap["name"]]["weight"] = (($productsmap["weight"])?$productsmap["weight"]:"");

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by fotisman01 on Sat, 2015-02-21 15:24

Thank you David. Worked perfectlty! One small mistake with the second replacement in admin/productsmap_configure.php at line 103:

widget_textBox("Custom Image URL","image_url",FALSE,$productmap["image_url"],"",6);
widget_textBox("Custom Age","age",FALSE,$productmap["age"],"",6);
widget_textBox("Custom Weight","weight",FALSE,$productmap["weight"],"",6);

was missing the " after age and weight in your code above (in case someone else want to use it)!

Now i see the new fields in product mapping and in the database!

So now how do I add them in search filters widget in joomla??? Can you please help me?

Thank you David!!!

Submitted by support on Mon, 2015-02-23 09:15

Ooops, thanks for that corrected above!

To add filters, it is essentially just a case of duplicating the existing code for, say brandFilter and duplicating for each custom field, so in the plugin file plugins/system/plg_pto_system/pto_search.php, look for the following code at line 130:

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

...and REPLACE with:

  $pto_brandFilter = (isset($pto_brandFilter)?$pto_brandFilter:"");
  global $pto_ageFilter;
  global $pto_weightFilter;
  $pto_ageFilter = (isset($pto_ageFilter)?$pto_ageFilter:"");
  $pto_weightFilter = (isset($pto_weightFilter)?$pto_weightFilter:"");

And then the following code at line 162:

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

...and REPLACE with:

  if ($pto_brandFilter)
  {
    $priceWhere .= " AND brand = '".$db->escape($pto_brandFilter)."' ";
  }
  if ($pto_ageFilter)
  {
    $priceWhere .= " AND age = '".$db->escape($pto_ageFilter)."' ";
  }
  if ($pto_weightFilter)
  {
    $priceWhere .= " AND weight = '".$db->escape($pto_weightFilter)."' ";
  }

And then the following code at line 439:

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

...and REPLACE with:

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

And then the following code at line 654:

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

...and REPLACE with:

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

And finally look for the following code at line 912:

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

...and REPLACE with:

  $sql = "SELECT DISTINCT(age) FROM `".$pto_config_databaseTablePrefix."products` WHERE ".$pto_searchWhere." AND age <> '' ORDER BY age";
  $db->setQuery($sql);
  $pto_filterResults = $db->loadObjectList();
  if (($parts[0] != "age") && count($pto_filterResults))
  {
    $html_ages = "";
    foreach($pto_filterResults as $product)
    {
      $html_ages .= "<option ".($pto_ageFilter==$product->age?"selected='selected'":"")." value='".htmlentities($product->age,ENT_QUOTES)."'>".$product->age."</option>";
    }
    $html = str_replace("%AGES%",$html_ages,$html);
    $html = str_replace("%IF_AGES%","",$html);
    $html = str_replace("%ENDIF_AGES%","",$html);
  }
  else
  {
    $html = preg_replace('/%IF_AGES%(.*)%ENDIF_AGES%/','',$html);
  }
  $sql = "SELECT DISTINCT(weight) FROM `".$pto_config_databaseTablePrefix."products` WHERE ".$pto_searchWhere." AND weight <> '' ORDER BY weight";
  $db->setQuery($sql);
  $pto_filterResults = $db->loadObjectList();
  if (($parts[0] != "weight") && count($pto_filterResults))
  {
    $html_weights = "";
    foreach($pto_filterResults as $product)
    {
      $html_weights .= "<option ".($pto_weightFilter==$product->weight?"selected='selected'":"")." value='".htmlentities($product->weight,ENT_QUOTES)."'>".$product->weight."</option>";
    }
    $html = str_replace("%WEIGHTS%",$html_weights,$html);
    $html = str_replace("%IF_WEIGHTS%","",$html);
    $html = str_replace("%ENDIF_WEIGHTS%","",$html);
  }
  else
  {
    $html = preg_replace('/%IF_WEIGHTS%(.*)%ENDIF_WEIGHTS%/','',$html);
  }
  $html = str_replace("%PTO_Q%",$pto_q,$html);

Finally, the Main / Search Filters template must be updated to include the placholders and text for the new filters, so if you go to /administrator/ and then Components > PriceTapestry.org, scroll down to the Main / Search Filters template and insert just below the existing code for the brand filter:

%IF_AGES%
Age:<br />
<select name='pto_ageFilter'>
  <option value=''>All</option>
  %AGES%
</select>
<br />
%ENDIF_AGES%
%IF_WEIGHTS%
Weight:<br />
<select name='pto_weightFilter'>
  <option value=''>All</option>
  %WEIGHTS%
</select>
<br />
%ENDIF_WEIGHTS%

Cheers,
David.
--
PriceTapestry.com

Submitted by fotisman01 on Tue, 2015-02-24 14:04

Tryed the solution adove, but the search.php was changed in the past.

The search is pased below. Do you think it can imported easily?

{code saved}