You are here:  » Split CSV


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

Submitted by sirmanu on Mon, 2023-02-20 09:12

Hi David.

I have another merchant which has this same problem.

The problem is that the splitFields are different (this time, they have dimensions and ean, in lower case, splitted by ";").

Also, the feed is zipped.

How can I modify the script in order to works with also this merchant?

Best regards.

Submitted by sirmanu on Mon, 2023-02-20 09:16

EDIT:

Maybe it can be done with a custom filter? Or it doesn't make sense?

Submitted by support on Wed, 2023-03-01 09:31

Hi,

Can you copy a couple of example lines from the new (unzipped) feed and I'll see if the above can be converted easily to work with it...

Thanks,
David.
--
PriceTapestry.com