You are here:  » Custom Fields for Product Mapping


Custom Fields for Product Mapping

Submitted by biglion on Mon, 2011-07-18 16:14 in

Is there a way to add a few more custom fields to the Product Mapping page? Fields for "Meta Title", "Meta Description", and others would be great for SEO.

Thanks.

Submitted by support on Mon, 2011-07-18 16:52

Hi biglion,

Are you using the new 12/10B distribution with the existing custom override fields for Product Mapping or do you want to add this to an earlier distribution?

If you could let me know I'll post the code first thing tomorrow for you...

Cheers,
David.
--
PriceTapestry.com

Submitted by biglion on Mon, 2011-07-18 17:04

It's the latest 12/10B distribution with the custom field already there. I just wanted to add a few additional custom fields.

Submitted by support on Tue, 2011-07-19 07:47

Hi biglion,

To add meta_title, meta_description and meta_keywords as custom fields, first locate the following code at line 125 of admin/productsmap_configure.php

    print "Custom Image URL:<br />";
    print "<input type='text' name='image_url' value='".widget_safe($productmap["image_url"])."' />";
    print "<br /><br />";

...and REPLACE with:

    print "Custom Image URL:<br />";
    print "<input type='text' name='image_url' value='".widget_safe($productmap["image_url"])."' />";
    print "<br /><br />";
    print "Meta Title:<br />";
    print "<input type='text' name='meta_title' value='".widget_safe($productmap["meta_title"])."' />";
    print "<br /><br />";
    print "Meta Description:<br />";
    print "<input type='text' name='meta_description' value='".widget_safe($productmap["meta_description"])."' />";
    print "<br /><br />";
    print "Meta Keywords:<br />";
    print "<input type='text' name='meta_keywords' value='".widget_safe($productmap["meta_keywords"])."' />";
    print "<br /><br />";

Next, look for the following code at line 39:

    if (!isset($_POST["image_url"])) $_POST["image_url"] = "";

...and REPLACE with:

    if (!isset($_POST["image_url"])) $_POST["image_url"] = "";
    if (!isset($_POST["meta_title"])) $_POST["meta_title"] = "";
    if (!isset($_POST["meta_description"])) $_POST["meta_description"] = "";
    if (!isset($_POST["meta_keywords"])) $_POST["meta_keywords"] = "";

...and finally the following code at line 74:

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

...REPLACE with:

     image_url = '".database_safe(widget_posted($_POST["image_url"]))."',
     meta_title = '".database_safe(widget_posted($_POST["meta_title"]))."',
     meta_description = '".database_safe(widget_posted($_POST["meta_description"]))."',
     meta_keywords = '".database_safe(widget_posted($_POST["meta_keywords"]))."'

Now, in products.php, to make use of the stored meta value, locate the following code at line 64:

  $header["title"] = htmlentities($q,ENT_QUOTES,$config_charset);
  $header["meta"]["description"] = translate("Price search results for")." ".htmlentities($q,ENT_QUOTES,$config_charset);
  $header["meta"]["keywords"] = htmlentities($q,ENT_QUOTES,$config_charset);

...and REPLACE with:

  $meta_sql = "SELECT * FROM `".$config_databaseTablePrefix."productsmap` WHERE name='".database_safe($product["products"][0]["name"])."' LIMIT 1";
  database_querySelect($meta_sql,$meta_rows);
  if (isset($meta_rows[0]["meta_title"]) && ($meta_rows[0]["meta_title"]!=""))
  {
    $header["title"] = htmlentities($meta_rows[0]["meta_title"],ENT_QUOTES,$config_charset);
  }
  else
  {
    $header["title"] = htmlentities($q,ENT_QUOTES,$config_charset);
  }
  if (isset($meta_rows[0]["meta_description"]) && ($meta_rows[0]["meta_description"]!=""))
  {
    $header["meta"]["description"] = $meta_rows[0]["meta_description"];
  }
  else
  {
    $header["meta"]["description"] = translate("Price search results for")." ".htmlentities($q,ENT_QUOTES,$config_charset);
  }
  if (isset($meta_rows[0]["meta_keywords"]) && ($meta_rows[0]["meta_keywords"]!=""))
  {
    $header["meta"]["keywords"] = $meta_rows[0]["meta_keywords"];
  }
  else
  {
    $header["meta"]["keywords"] = htmlentities($q,ENT_QUOTES,$config_charset);
  }

The above means that you won't wipe out the automatically generated title, description and keywords by not having yet entered values for them within product mapping - they will only be used if they exist and are not empty...

Finally, a dbmod.php is required to add the new fields to the `productsmap` table, as follows:

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

Create a file containing the above as dbmod.php, upload to the main Price Tapestry installation folder, browse to dbmod.php once and then delete the file.

You'll notice that for each field there is an identical block of code within the above modifications so to extend the example to additional custom fields simply copy the lines associated with an individual field and replace with your additional custom field names, but take care for example in the SQL code modification where there are commas on the end of each line except the last - let me know of course if you're not sure.

In terms of using the additional fields on the products page; anywhere after the final modification above the $meta_rows array is in scope, and you can access the new fields using

$meta_rows[0]["custom_field_name"]

Within the HTML module (html/product.php) let's say you wanted to the print the value of custom_field_name (if exists), you would use as follows (including PHP tags as the majority of that file is HTML mode)

<?php
  
if (isset($meta_rows[0]["custom_field_name"]) && ($meta_rows[0]["custom_field_name"]!=""))
  {
    print 
$meta_rows[0]["custom_field_name"];
  }
?>

I appreciate that's quite an extensive group of modifications if you're not sure about any stage and would like me to send you the modified files based on the distribution versions just let me know...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by biglion on Tue, 2011-07-19 16:07

This is exactly what I needed. Excellent support as usual. Thanks!

There were 2 minor typos which caused errors, but I managed to find them. In case anyone else needs this mod...

in products.php
$header["meta"]["description"] = $meta_rows[0]["meta_description"]
should be
$header["meta"]["description"] = $meta_rows[0]["meta_description"];

in productsmap_configure.php
meta_keywrods = '".database_safe(widget_posted($_POST["meta_keywords"]))."'
should be
meta_keywords = '".database_safe(widget_posted($_POST["meta_keywords"]))."'

Submitted by support on Tue, 2011-07-19 16:19

Thanks biglion,

Typos corrected in the original reply...

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Tue, 2011-07-26 09:38

Hi David,
This new Product Mapping seem just what I need. A couple of questions though.
1 Would it be possible to add a preferred merchant field on this mapping (other posts on this subject elsewhere) in the same way.
2 If there is a custom product with description , image etc, what happens if that product is not on the feeds when they are updated, such as a discontinued model, does it disappear or stay on the website incorrectly?
Thanks
Ben

Submitted by support on Tue, 2011-07-26 10:56

Hi Ben,

Custom fields in Product Mapping only apply to "live" products, e.g. they must exist in the database so if the products no longer appear in the feed (and therefore aren't picked up by the mapping) then they will disappear at the next import.

Note however that the mappings remain in place of course, they are not deleted at all unless you delete them manually from the Product Mapping tool in /admin/, so if the products subsequently return in a later update then they will re-appear using the same overrides etc...

Regarding preferred merchant, is the objective to be able to enter a merchant name on the configuration page for a Product Mapping, and then have that merchant's description used on the product page if there is a preferred merchant specified?

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Tue, 2011-07-26 11:35

Hi David
Thanks for explaining the product mapping.
In terms of the preferred merchant, the default setting is that the cheapest merchant link appears next to the photo and description with the price comparison below. In my situation the prices are only guide prices, so I would prefer the main link to send to a preferred merchant, especially if I am using their photo as I could have issues otherwise. So in the admin product mapping page I would just have a box to type in preferred merchant, same as the meta , image etc. I hope I have explained it ok.
Thanks
Ben

Submitted by support on Tue, 2011-07-26 12:16

Hi Ben,

I see - that's relatively straight forward to do. Based on the instructions above for adding custom fields to Product Mapping (e.g. meta_title) first add the field "pref_merchant".

With the same changes in place as described above to pull in the meta_ details in products.php, the changes to actually use pref_merchant can then be added to html/product.php. Look for the following code at line 2:

  <?php $mainProduct $product["products"][0]; ?>

....and REPLACE with:

  <?php
  $mainProduct = $product["products"][0];
  if ($meta_rows[0]["pref_merchant"])
  {
    foreach($product["products"] as $k => $v)
    {
      if($v["merchant"]==$meta_rows[0]["pref_merchant"])
      {
        $mainProduct = $product["products"][$k];
        break;
      }
    }
  }
  ?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by brentmitchell on Sun, 2011-08-28 21:04

hey david,

i tried to do this mod, but couldn't find this part:

...and finally the following code at line 74:

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

image_url = '".database_safe(widget_posted($_POST["image_url"]))."',
meta_title = '".database_safe(widget_posted($_POST["meta_title"]))."',
meta_description = '".database_safe(widget_posted($_POST["meta_description"]))."',
meta_keywords = '".database_safe(widget_posted($_POST["meta_keywords"]))."'

Did something change with the code?

Submitted by support on Mon, 2011-08-29 07:02

Hello Brent,

It's line 74 in admin/productsmap_configure.php where you'll find

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

...and then replace with:

              image_url = '".database_safe(widget_posted($_POST["image_url"]))."',
              meta_title = '".database_safe(widget_posted($_POST["meta_title"]))."',
              meta_description = '".database_safe(widget_posted($_POST["meta_description"]))."',
              meta_keywords = '".database_safe(widget_posted($_POST["meta_keywords"]))."'

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Fri, 2011-09-09 09:58

Hi David, Thanks for the reply about Preferred Merchant, i have only just got around to try and implement it and can't get it to work.
I have added the other fields (meta_title, meta_description and meta_keywords) and have them working, but when I did exactly the same to admin/productsmap_configure.php for some reason when I type in the pref_merchant field it won't write to the database table and if I manually add the merchant in phpmyadmin, it remembers it but does not display it and I change it in pt admin pages, again it does not remember it.
Do I need to add a pref_merchant field to products or feeds table or is it that I have not done other preferred merchant mods, such as http://www.pricetapestry.com/node/2492.
I enclose admin/productsmap_configure.php and my productsmap table structure to see if there are errors there.
Thanks
Ben
{code saved}

Submitted by support on Mon, 2011-09-12 08:58

Hi Ben,

I spotted an error in the line that constructs the SQL which will be why it's not updating; the line should be:

    $sql = "UPDATE `".$config_databaseTablePrefix."productsmap` SET
              alternates = '".database_safe($alternates)."',
              description = '".database_safe(widget_posted($_POST["description"]))."',
              category = '".database_safe(widget_posted($_POST["category"]))."',
              brand = '".database_safe(widget_posted($_POST["brand"]))."',
              image_url = '".database_safe(widget_posted($_POST["image_url"]))."',
              meta_title = '".database_safe(widget_posted($_POST["meta_title"]))."',
              meta_description = '".database_safe(widget_posted($_POST["meta_description"]))."',
              meta_keywords = '".database_safe(widget_posted($_POST["meta_keywords"]))."',
              pref_merchant = '".database_safe(widget_posted($_POST["pref_merchant"]))."'
              WHERE id ='".database_safe($id)."'";

(it was just a comma missing on the end of the meta_keywords line - that should be all it is)

Cheers,
David.
--
PriceTapestry.com

Submitted by affiliben on Mon, 2011-09-12 09:22

Hi David
Thanks very much, excellent service as usual
Ben

Submitted by seceleanu on Wed, 2017-03-01 18:24

Hi David,

Do you have an update on this? I'm using the latest version - 16/10A - and I really need to be able to add custom fields. Is there any other solution?

Thanks!
MIhai

Submitted by support on Thu, 2017-03-02 08:49

Hello MIhai and welcome to the forum.

You'll find updated instructions for this modification now in this thread...

Cheers,
David.
--
PriceTapestry.com