You are here:  » XML with Child


XML with Child

Submitted by marco.saiu on Fri, 2017-12-29 11:42 in

Hello David,

i have xml with Child @1 @2 @3 @4 ...

I can import this data?

FEATURES
FEATURES/FEATURE
FEATURES/FEATURE-NAME Brand
FEATURES/FEATURE-VALUE Gucci
FEATURES/FEATURE@1
FEATURES/FEATURE@1-NAME Colour
FEATURES/FEATURE@1-VALUE Black
FEATURES/FEATURE@2
FEATURES/FEATURE@2-NAME Type
FEATURES/FEATURE@2-VALUE Jeans

This features change per feed not have order and not know all features in catalog.

Thanks,
Marco Saiu

Submitted by support on Fri, 2017-12-29 13:57

Hello Marco,

I have come across variations of this a few times - the structure is easy to parse into an array at import time and a nice way to do this is by an extension to the function that processes %FIELDNAME% placeholders for the Text Before / Text After filters.

To try this, first edit includes/filter.php and look for the filter_recordPlaceholders() function beginning at line 2:

  function filter_recordPlaceholders($text)
  {
    global $filter_record;
    if (strpos($text,"%")!==FALSE)
    {
      foreach($filter_record as $k => $v)
      {
        $text = str_replace("%".$k."%",$v,$text);
      }
    }
    return $text;
  }

...and REPLACE with the following new version:

  function filter_recordPlaceholders($text)
  {
    global $filter_record;
    $features = array();
    $i = 0;
    $p = "";
    while(1)
    {
      if ($i) $p = "@".$i;
      if (!isset($filter_record["FEATURES/FEATURE".$p."-NAME"])) break;
      $name = $filter_record["FEATURES/FEATURE".$p."-NAME"];
      $value = $filter_record["FEATURES/FEATURE".$p."-VALUE"];
      $features[$name] = $value;
      $i++;
    }
    if (strpos($text,"{")!==FALSE)
    {
      foreach($features as $k => $v)
      {
        $text = str_replace("{".$k."}",$v,$text);
      }
    }
    if (strpos($text,"%")!==FALSE)
    {
      foreach($filter_record as $k => $v)
      {
        $text = str_replace("%".$k."%",$v,$text);
      }
    }
    return $text;
  }

With that in place, you can now use curly brackets and the feature name to swap out the feature value in Text Before / Text After filters. Using Brand as an example, when registering the feed leave Brand unmapped, and then after registration click Filters alongside the feed filename from /admin/ home page and add a new Text Before filter to the Brand field. In the text box on the configuration page for the filter, enter:

{Brand}

...and at the next import it will be swapped out with the brand value from the Feature array in the XML...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com