You are here:  » Drop record of field without adding that field to database


Drop record of field without adding that field to database

Submitted by sirmanu on Wed, 2016-11-16 23:03 in

Hi.

I have a feed with a lot of fields.

What I want to do is to drop the record if one field of the .csv not contains some string. The thing is that I don't want to add this field to the database (according to these instructions) because is a field that I am not going to use (only for dropping) and also these field is just for this merchant without any meaning.

Is it possible?

Submitted by support on Thu, 2016-11-17 15:22

Hi,

Sure - add the following code to includes/filter.php to add a new "Drop Record Full" filter, that will scan the entire record for the string, and drop if found. The filter can be attached to any field (it doesn't matter which), or as a Global Filter:

  /*************************************************/
  /* dropRecordFull */
  /*************************************************/
  $filter_names["dropRecordFull"] = "Drop Record Full";
  function filter_dropRecordFullConfigure($filter_data)
  {
    widget_textBox("Drop record if any field contains text","text",FALSE,$filter_data["text"],"",6);
  }
  function filter_dropRecordFullValidate($filter_data)
  {
  }
  function filter_dropRecordFullExec($filter_data,$text)
  {
    global $filter_dropRecordFlag;
    global $filter_record;
    if($filter_dropRecordFlag)
    {
      return $text;
    }
    elseif($filter_data["text"])
    {
      $scan = implode(" ",$filter_record);
      $filter_dropRecordFlag = (strstr($scan,$filter_data["text"]) !== FALSE);
    }
    return $text;
  }

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Fri, 2016-11-18 08:14

Mmmhh, I think I have not explained very well, sorry. For example, I have a merchant which one of their fields is <isValid>

The possible values are yes/no/Other/Third Party
<isValid> is not a column in pt_products

What I want to do is to drop records when isValid is not Yes.

This would be easy to do adding a new column for this field in pt_products. But as this field is only used for one of my merchants, I think is not a good idea because also is not going to be used.

The question is, how to achieve that?

Submitted by support on Fri, 2016-11-18 08:41

Hi,

Here's a filter to try - Drop Record If Not (Parsed) - add the following code to includes/filter.php

  /*************************************************/
  /* dropRecordIfNotParsed */
  /*************************************************/
  $filter_names["dropRecordIfNotParsed"] = "Drop Record If Not (parsed)";
  function filter_dropRecordIfNotParsedConfigure($filter_data)
  {
    widget_textBox("Field Name","field",FALSE,$filter_data["field"],"",6);
    widget_textBox("Text","text",FALSE,$filter_data["text"],"",6);
  }
  function filter_dropRecordIfNotParsedValidate($filter_data)
  {
  }
  function filter_dropRecordIfNotParsedExec($filter_data,$text)
  {
    global $filter_dropRecordFlag;
    global $filter_record;
    if ($filter_dropRecordFlag)
    {
      return $text;
    }
    $filter_dropRecordFlag = (strstr($filter_record[$filter_data["field"]],$filter_data["text"]) == FALSE);
    return $text;
  }

Attach the filter to any field (doesn't matter which) and then on the configuration page, enter the field name to test exactly as it appears in the Sample Data shown below the form on Feed Registration Step 2, and the text to match.

Note that field names are capitalised for XML feeds, so the field name to use in your case would be ISVALID and the text "Yes" (without the quotes). As it stands the above would be case sensitive, but you could make it case insensitive by changing strstr to the case insensitive version stristr if required.

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Fri, 2016-11-18 17:31

Ok, I am going to try it!
This merchant is very weird. Also is not always present, and when is not, is like if is No. So I added this lines

if (!isset($filter_record[$filter_data["field"]]))
$filter_dropRecordFlag = FALSE;
else
$filter_dropRecordFlag = (strstr($filter_record[$filter_data["field"]],$filter_data["text"]) == FALSE);
    return $text;

This merchant (is from CJ), it also have another issue. I have looked into the forum and I saw a solution which implies to modify admin.php
I want to know if it is possible to do with a filter

This merchant have price, but sometimes also it has saleprice. I cannot select for field saleprice because generally only appears price.

So what I want to do, is to replace price when saleprice is set. Any easy way to achieve this?

Submitted by support on Fri, 2016-11-18 17:48

Hi,

Have a go with something like;

  /*************************************************/
  /* checkSalePrice */
  /*************************************************/
  $filter_names["checkSalePrice"] = "Check Sale Price";
  function filter_checkSalePriceConfigure($filter_data)
  {
    widget_textBox("Field Name","field",FALSE,$filter_data["field"],"",6);
  }
  function filter_checkSalePriceValidate($filter_data)
  {
  }
  function filter_checkSalePriceExec($filter_data,$text)
  {
    global $filter_record;
    if (isset($filter_record[$filter_data["field"]]) && $filter_record[$filter_data["field"]])
    {
      return $filter_record[$filter_data["field"]];
    }
    return $text;
  }

Attach to Price field, and in the Field Name box on the configuration page as above enter the field name as appears in the Sample Data on Feed Registration Step 2 (don't forget that you can navigate records if the sale price field is not present in the first record). The _Exec function will check both if exists and if so if there is a value present before applying.

Cheers,
David.
--
PriceTapestry.com

Submitted by sirmanu on Thu, 2017-03-23 14:01

Hi David.
I have done a litte modification.
Rather than If not, I want to discard if matches.
I have a field called "availability" (CSV). I am writing "NO STOCK" to discard no stock products.
Is this code correct?

  $filter_names["dropRecordIfParsed"] = "Drop Record If (parsed)";
  function filter_dropRecordIfParsedConfigure($filter_data)
  {
    widget_textBox("Field Name","field",FALSE,$filter_data["field"],"",6);
    widget_textBox("Text","text",FALSE,$filter_data["text"],"",6);
  }
  function filter_dropRecordIfParsedValidate($filter_data)
  {
  }
  function filter_dropRecordIfParsedExec($filter_data,$text)
  {
    global $filter_dropRecordFlag;
    global $filter_record;
    if ($filter_dropRecordFlag)
    {
      return $text;
    }
    if (!isset($filter_record[$filter_data["field"]]))
      $filter_dropRecordFlag = TRUE;
    else{
      $filter_dropRecordFlag = (stristr($filter_record[$filter_data["field"]],$filter_data["text"]) == TRUE);
      //var_dump($filter_dropRecordFlag);
    }
    return $text;
  }

Submitted by support on Fri, 2017-03-24 14:58

Hi,

The comparison would miss the tested value beginning at position 0, in place of this line:

$filter_dropRecordFlag = (stristr($filter_record[$filter_data["field"]],$filter_data["text"]) == TRUE);

...use:

$filter_dropRecordFlag = (stristr($filter_record[$filter_data["field"]],$filter_data["text"]) !== FALSE);

Hope this helps!

Cheers,
David.
--
PriceTapestry.com