You are here:  » Multiple insert per record


Multiple insert per record

Submitted by sirmanu on Wed, 2018-10-17 06:24 in

Hi David. I want to know how to insert multiple products per record.
I have some feeds which variants are separated per a comma.

All the fields are the same except the UPC and the SIZE.

size (field_size): XS,S,M,L,XL
upc (field_uid): 191530885314,191530885112,191530885214,191530885213,191530885310

The rest of the fields are the same. As you can see, the variant is separated by a comma. So one product would be size XS with upc 191530885394. The rest of the fields would be the same (yes, same name).

Which is the easiest way to add this variations individually? Maybe a filter?

Submitted by support on Wed, 2018-10-17 10:00

Hi,

I know that your import process has been modified to do a batch import, so as long as dupe_hash doesn't need to be modified it should be just a foreach() loop around the code you are using to populate $admin_importRecordValues. So where you currently have the following code beginning at around line 533:

    $importRecordValues = array();
    foreach($importRecord as $field => $v)
    {
      $importRecordValues[] = "'".database_safe($v)."'";
    }
    $admin_importRecordValues[] = "(".implode(",",$importRecordValues).")";

...REPLACE with:

    $sizes = explode(",",$importRecord["size"]);
    $uids = explode(",",$importRecord["uid"]);
    foreach($sizes as $k => $size)
    {
      $importRecord["size"] = $sizes[$k];
      $importRecord["uid"] = $uids[$k];
      $importRecordValues = array();
      foreach($importRecord as $field => $v)
      {
        $importRecordValues[] = "'".database_safe($v)."'";
      }
      $admin_importRecordValues[] = "(".implode(",",$importRecordValues).")";
    }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Wed, 2018-10-17 11:18

Excellent! I didn't remember the unique dupe hash. So I added:

$importRecord["size"] = $sizes[$k];
$importRecord["uid"] = $uids[$k];
$importRecord["dupe_hash"] = md5($dupe_key.$sizes[$k]);

Submitted by sirmanu on Wed, 2018-10-17 11:19

Also, a small verification for merchants that doesn't have commas.

$sizes = explode(",",$importRecord["size"]);
$uids = explode(",",$importRecord["uid"]);

if (count($uids) > 1 && count($sizes) === count($uids)) {

foreach($sizes as $k => $size)
{
$importRecord["size"] = $sizes[$k];
$importRecord["uid"] = $uids[$k];
$importRecord["dupe_hash"] = md5($dupe_key.$sizes[$k]);
$importRecordValues = array();
foreach($importRecord as $field => $v)
{
$importRecordValues[] = "'".database_safe_fast($v)."'";
}
$admin_importRecordValues[] = "(".implode(",",$importRecordValues).")";

}

} else {

// previous code
}