You are here:  » Extract Values From Product Feed


Extract Values From Product Feed

Submitted by Stew on Thu, 2015-03-26 08:13 in

Hi David,

Hope all well, I have a merchant feed that has bundled a lot of useful information within one column named 'fields'.

Within that one column is for example:

Colours:Black;Department:Tops & T-Shirts,Camisoles and Vests;Gender:female;MerchantCategoryName:Lingerie > Camisoles & Vests;Product Rating:5 out of 5;Product Style:Non-Padded,Thermal

For now am just trying to extract the colour and add it into my already created custom colour field.

What's the best way to do this, is it to use something like the scan & set (http://www.pricetapestry.com/node/5126) but instead of name & description use the collumn 'fields' - or is there a better way?

Many Thanks,

Stew

Submitted by support on Thu, 2015-03-26 10:17

Hi Stew,

As the format appears to be predictable - a semi-colon separated list of key:value pairs, it would be straight forward to create a generic filter to process this format and return the value of a selected key. If you add the following new filter code to includes/filter.php:

  /*************************************************/
  /* keyValue */
  /*************************************************/
  $filter_names["keyValue"] = "Key Value Extract";
  function filter_keyValueConfigure($filter_data)
  {
    widget_textBox("Primary Separator","separator1",TRUE,$filter_data["separator1"],"",3);
    widget_textBox("Key:Value Separator","separator2",TRUE,$filter_data["separator2"],"",3);
    widget_textBox("Key","key",TRUE,$filter_data["key"],"",3);
  }
  function filter_keyValueValidate($filter_data)
  {
    if ($filter_data["separator1"]=="") widget_errorSet("separator1","required field");
    if ($filter_data["separator2"]=="") widget_errorSet("separator2","required field");
    if ($filter_data["key"]=="") widget_errorSet("key","required field");
  }
  function filter_keyValueExec($filter_data,$text)
  {
    $values = array();
    $parts1 = explode($filter_data["separator1"],$text);
    foreach($parts1 as $kv)
    {
      $parts2 = explode($filter_data["separator2"],$kv);
      $values[$parts2[0]] = $parts2[1];
    }
    return $values[$filter_data["key"]];
  }

With that in place, register this field as your `color` field, and then add a new Key Value Extract filter to the color field configured as follows:

Primary Separator:

;

Key:Value Separator:

:

Key:

Colours

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by Stew on Fri, 2015-03-27 11:59

Hi David - that's fantastic,

Implemented the code and worked first time!

Many Thanks

Stew