You are here:  » Text in price field

Support Forum



Text in price field

Submitted by marco on Thu, 2008-03-20 16:34 in

Hello,

Can't figure this one out.

The magazine merchant which feed i would like to use has text in the price field, like: "3 issues for € 7,50".

I would like to use the same field (without conversion) in another field.

How can i import this one in the PRICE field with the correct price and still use the full text for another field?

Thanks,

Submitted by support on Thu, 2008-03-20 17:02

Hi Marco,

Is every price field exactly the same format, or is there different wording being used depending on the deal?

If there is different text, can you see a way to always get the price - for example the price is always at the end?

It will need a special filter writing for this, but the fields would need to be consistent...

Cheers,
David.

Submitted by marco on Thu, 2008-03-20 20:03

Hi David,

About 95% of the feed is in this format with the price at the end.

About 3% has some text behind the price like "3 issues for € 7,50 + DVD"
About 2% has only text like "4 free numbers".
I would not mind if those 5% of record where dropped or would get a price of zero.

It would help me a lot to get the 95% right.

Thanks,

Marco

Submitted by support on Fri, 2008-03-21 13:40

Hello Marco,

I've thought of a tidy way to do this that should work well. It involves adding a new filter to your site - Extract Price - that will break the string up into words, and then look for a , or a . in each word, which would indicate that word as being the price part of the string.

However, in order to do this, you need to disable the default filtering of the price field which is done by the following code on line 168 of includes/admin.php:

$record[$admin_importFeed["field_price"]] = tapestry_decimalise($record[$admin_importFeed["field_price"]]);

Delete or comment out the above line to permit the price value to progress further so that it can be processed by the new filter. To add the new filter, add the code below to includes/filter.php (make sure you paste the code within the PHP tags).

You can then go to the filters section for the feed that has these price values, and add a new "Extract Price" filter to the "Price" field. Then import the feed as normal and the price values should have been correctly imported.

  /*************************************************/
  /* ExtractPrice */
  /*************************************************/
  $filter_names["extractPrice"] = "Extract Price";
  function filter_extractPriceConfigure($filter_data)
  {
    print "<p>There are no additional configuration parameters for this filter.</p>";
  }
  function filter_extractPriceValidate($filter_data)
  {
  }
  function filter_extractPriceExec($filter_data,$text)
  {
    $words = explode(" ",$text);
    foreach($words as $word)
    {
      if (strpos($word,".") || strpos($word,","))
      {
        return $word;
      }
    }
    return "0.00";
  }

As it stands, this would import any product with a price that can't be extracted as 0.00. If this isn't desirable, it is easy to make it drop these records instead. To do this, change the filter_extractPriceExec function as follows instead of the above version:

  function filter_extractPriceExec($filter_data,$text)
  {
    global $filter_dropRecordFlag;
    $words = explode(" ",$text);
    foreach($words as $word)
    {
      if (strpos($word,".") || strpos($word,","))
      {
        return $word;
      }
    }
    $filter_dropRecordFlag = TRUE;
    return $text;
  }

Hope this helps!

Cheers,
David.

Submitted by marco on Tue, 2008-03-25 08:50

Thanks David, the price extraction filter works great.

There is only one thing with the format of the price. The price is rounded down on import, for example: a price of 7,50 is imported as 7.00.
How can i change that?

Marco.

Submitted by support on Tue, 2008-03-25 10:30

Hi Marco,

I see why that is happening. This is normally handled correctly by the tapestry_decimalise() function, but since that has been removed in this modification a slight change is needed to the filter to replace the "," with a "." if necessary (which is what the tapestry_decimalise() function would have done normally). Here's a new version of the filter:

  /*************************************************/
  /* ExtractPrice */
  /*************************************************/
  $filter_names["extractPrice"] = "Extract Price";
  function filter_extractPriceConfigure($filter_data)
  {
    print "<p>There are no additional configuration parameters for this filter.</p>";
  }
  function filter_extractPriceValidate($filter_data)
  {
  }
  function filter_extractPriceExec($filter_data,$text)
  {
    $words = explode(" ",$text);
    foreach($words as $word)
    {
      if (strpos($word,".") || strpos($word,","))
      {
        return str_replace(",",".",$word);
      }
    }
    return "0.00";
  }

Cheers,
David.