You are here:  » Split CSV

Active Forum Topics


Split CSV

Submitted by sirmanu on Tue, 2022-08-16 15:34 in

Hi David.

I have an issue regarding product feed for a merchant.

They are grouping in a CSV several variations per product, separated via comma.s Please, see next image:

{link saved}

This is difficult to process. According to the image provided, one product must be EAN 4056561961584 with sie 5-6Y, quantity 0, other EAN 4056561961539....

Do you have any script that can work after automation_tool.php so the feed is one variation per CSV row?

Thank you!

Submitted by support on Thu, 2022-08-18 14:06

Hi,

What is the feed format (as auto-detected on Feed Registration Step 1)?

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Fri, 2022-08-19 08:53

csv|44|1|34

Submitted by support on Sun, 2022-08-21 08:41

Hi,

Here's something to try, create the following file in your scripts/ folder as splitfetch.php:

<?php
  
require("../includes/MagicParser.php");
  
$url "https://example.com/path/to/feed.csv";
  
$splitFields = array("quantity","size","EAN");
  
$nameField "name";
  
$first TRUE;
  function 
myRecordHandler($record)
  {
    global 
$splitFields;
    global 
$nameField;
    global 
$first;
    if (
$first)
    {
      
$fieldNames = array();
      foreach(
$record as $k => $v)
      {
        
$fieldNames[] = $k;
      }
      print 
implode("\t",$fieldNames).PHP_EOL;
      
$first FALSE;
    }
    
$name $record[$nameField];
    
$newRecord $record;
    foreach(
$splitFields as $splitField)
    {
      
$splitValues[$splitField] = explode(",",$record[$splitField]);
    }
    
$j count($splitValues[$splitField]);
    for(
$i=0;$i<$j;$i++)
    {
      
$newRecord[$nameField] = $name." ".$splitValues["EAN"][$i];
      foreach(
$splitFields as $splitField)
      {
        
$newRecord[$splitField] = $splitValues[$splitField][$i];
      }
      print 
implode("\t",$newRecord).PHP_EOL;
    }
  }
  
MagicParser_parse($url,"myRecordHandler","csv|44|1|34");
?>

Before uploading edit the URL at line 3 to the URL of your feed, edit the $splitFields array (line 4) to contain the list of fields that need to be exploded out and the name of the field containing the product name in $nameField at line 5.

The script will fetch the feed; and output separate rows for each of the $splitFields field values (and also append the EAN to the name otherwise they would be dropped as duplicates) as TAB separated values which I think is a safer format to use.

Finally, simply change the URL in your Automation Tool job to the URL of the new splitfetch.php script on your server, run the job and then re-register in the new format as required...

Hope this helps!

Cheers,
David.
--
PriceTapestry.com