You are here:  » Custom Product Fields on Regexp Mapped Products


Custom Product Fields on Regexp Mapped Products

Submitted by Erik on Sat, 2018-02-03 13:51 in

Hi,

Did not manage to find a concrete solution to this, so I figured I would check it out here, a couple questions:

1) Let's say I want to add a description field for a product manually to a Product Page created from Regexp

Would it suffice to add the custom field to the db table pt_productsmap_regexp and either adding it in phpmyadmin or via the regexp definition page?

I understand this page (https://www.pricetapestry.com/node/1547) describes product mapping but I'm wondering how I would manage to get custom fields for Regexp?

2) Similar to this, if I want to add custom fields for e.g. Smartphone products (Screen Size, OS, etc) - Would I do this be done in the same way as for above question?

I would like to be inputting the values myself on a grouped level (regex) instead of needing to get this metadata into every separate imported entry.

Thanks,
Erik

Submitted by support on Mon, 2018-02-05 08:42

Hello Eric,

Custom description and meta data support can be added to Product Mapping by RegExp easily.

First create and run the following dbmod.php script (run once from top level of Price Tapestry installation)

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."productsmap_regexp`
            ADD `description` TEXT NOT NULL default '',
            ADD `meta` TEXT NOT NULL default ''
            "
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

Then edit admin/productsmap_regexp_configure.php and look for the following code at line 16:

  $productmap = $rows[0];

...and REPLACE with:

  $productmap = $rows[0];
  if ($productmap["meta"])
  {
    $productmap["meta"] = unserialize($productmap["meta"]);
  }

And then the following code at (now) line 45:

    widget_validate("brand",FALSE,"normalised");

...and REPLACE with:

    widget_validate("brand",FALSE,"normalised");
    if (isset($config_productMeta))
    {
      $meta = array();
      foreach($config_productMeta as $field => $label)
      {
        $meta[$field] = $_POST[$field];
      }
      $meta = serialize($meta);
    }
    else
    {
      $meta = "";
    }

And then the following code at (now) line 74:

                `brand` = '".database_safe($_POST["brand"])."'

...and REPLACE with:

                `brand` = '".database_safe($_POST["brand"])."',
                `description` = '".database_safe($_POST["description"])."',
                `meta` = '".database_safe($meta)."'

And then the following code at (now) line 299:

        widget_textBox("Custom Brand","brand",FALSE,$productmap["brand"],"",12);

...and REPLACE with:

        widget_textBox("Custom Brand","brand",FALSE,$productmap["brand"],"",12);
        widget_textArea("Custom Description","description",FALSE,$productmap["description"],100,12);
        if (isset($config_productMeta))
        {
          foreach($config_productMeta as $field => $label)
          {
            widget_textBox($label,$field,FALSE,(isset($productmap["meta"][$field])?$productmap["meta"][$field]:""),"",6);
          }
        }

Then edit includes/admin.php and look for the following code at line 342:

          if ($admin_importProductMappingsRegExpOverrides[$v["name"]]["brand"]) $importRecord["brand"] = $admin_importProductMappingsRegExpOverrides[$v["name"]]["brand"];

...and REPLACE with:

          if ($admin_importProductMappingsRegExpOverrides[$v["name"]]["brand"]) $importRecord["brand"] = $admin_importProductMappingsRegExpOverrides[$v["name"]]["brand"];
          if ($admin_importProductMappingsRegExpOverrides[$v["name"]]["description"]) $importRecord["description"] = $admin_importProductMappingsRegExpOverrides[$v["name"]]["description"];

And finally the following code at line 662:

        $admin_importProductMappingsRegExpOverrides[$productsmap_regexp["name"]]["brand"] = (($productsmap_regexp["brand"])?$productsmap_regexp["brand"]:"");

...and REPLACE with:

        $admin_importProductMappingsRegExpOverrides[$productsmap_regexp["name"]]["brand"] = (($productsmap_regexp["brand"])?$productsmap_regexp["brand"]:"");
        $admin_importProductMappingsRegExpOverrides[$productsmap_regexp["name"]]["description"] = (($productsmap_regexp["description"])?$productsmap_regexp["description"]:"");

(use meta data exactly as described for standard Product Mapping)

Hope this helps!
Cheers,
David.
--
PriceTapestry.com

Submitted by Erik on Mon, 2018-02-05 21:16

Hi David,

Thank you for the awesome help as usual!

My only hiccup was at the end with the meta where I added one for screen as described in your link. The screen field is added to the regexp product mapping interface, only issue was that when saving it doesn't store the value, and is left blank.

Should a field be created in the db table for screen if I added the metadata for screen?

Submitted by support on Tue, 2018-02-06 11:52

Hello Erik,

My apologies, I had missed out documenting the second modification required to admin/productsmap_regexp_configure.php after working out the changes on my test server. Final mod required, look for the following code at line 45:

    widget_validate("brand",FALSE,"normalised");

...and REPLACE with:

    widget_validate("brand",FALSE,"normalised");
    if (isset($config_productMeta))
    {
      $meta = array();
      foreach($config_productMeta as $field => $label)
      {
        $meta[$field] = $_POST[$field];
      }
      $meta = serialize($meta);
    }
    else
    {
      $meta = "";
    }

(added above in original modification instructions)

Cheers,
David.
--
PriceTapestry.com

Submitted by Erik on Sat, 2018-02-10 10:49

Hi David,

I seemed to get everything working in terms of storing the value in the new "meta" field in pt_productsmap_regexp, for example one value is:

a:1:{s:6:"screen";s:4:"1.80";}

Now in product.php I have tried adding the line of code as recommended:

    <?php $screen tapestry_productMeta($product_main["name"],"screen"); ?>
    <?php if ($screen): ?>
    <p><strong>Screen Size:</strong> <?php print $screen?></p>
    <?php endif; ?>

tapestry_productMeta($product_main["name"],"screen") does not seem to grab the value, so I'm wondering if it needs to refer to the array from the example value above?

Submitted by support on Sat, 2018-02-10 11:55

Hello Erik,

Ah my apologies - a final couple of changes are required to fetch the meta data from productsmap_regexp instead of productsmap. To do this, the id of the productsmap_regexp record that created the name needs to be saved in the products table, so firstly create and run the following dbmod.php script to add a new `regexp_id` field:

<?php
  
require("includes/common.php");
  
$sql "ALTER TABLE `".$config_databaseTablePrefix."products`
            ADD `regexp_id` INT(11) NOT NULL"
;
  
database_queryModify($sql,$result);
  print 
"Done.";
?>

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

          $regexpMapped = TRUE;

...and REPLACE with:

          $regexpMapped = TRUE;
          $importRecord["regexp_id"] = $v["id"];

Edit includes/tapestry.php and look for the following code at line 537:

  function tapestry_productMeta($name,$field)

...and REPLACE with:

  function tapestry_productMeta($name,$field,$regexp_id=0)

And finally the following code at line 543:

    $sql = "SELECT meta FROM `".$config_databaseTablePrefix."productsmap` WHERE name='".database_safe($name)."'";

...and REPLACE with:

    if ($regexp_id)
    {
      $sql = "SELECT meta FROM `".$config_databaseTablePrefix."productsmap_regexp` WHERE id='".database_safe($regexp_id)."'";
    }
    else
    {
      $sql = "SELECT meta FROM `".$config_databaseTablePrefix."productsmap` WHERE name='".database_safe($name)."'";
    }

With the changes in place, run a full import to populate the new regexp_id field, and then in your display code, pass the value to the tapestry_productMeta function for example;

    <?php $screen tapestry_productMeta($product_main["name"],"screen",$product_main["regexp_id"]); ?>
    <?php if ($screen): ?>
    <p><strong>Screen Size:</strong> <?php print $screen?></p>
    <?php endif; ?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Erik on Sun, 2018-02-11 21:41

Perfect, this worked great! Thanks, as always, for the help!

Best,
Erik