You are here:  » Add a margin to a price during upload


Add a margin to a price during upload

Submitted by ROYW1000 on Mon, 2008-07-21 15:53 in

Hi

If I had a feed and wanted to add a margin to the price within the feed during update how would I do this.

Basically I would have a table called products and this would have field called price and margin.

If for example I had a margin of 10 within the products database how would I be able to get magicparser to read the 10 from within that product table per product and apply that margin and then insert the new price into the price table.

Thanks

Roy

Submitted by support on Mon, 2008-07-21 16:18

Hello Roy,

This isn't Magic Parser code specifically; but you include whatever PHP code you require within myRecordHandler() to do what you need to do with the data.

In this case, it should just be a question of performing a SELECT query to obtain the current product row; and then multiply the new price value by the margin(%) and insert as normal. The basic structure would be something like this; although your database code and queries would be somewhat different, but it should give you the idea...

function myRecordHandler($record)
{
  $result = mysql_query("SELECT * FROM products WHERE id='".mysql_escape_string($record["ID"])."'");
  $row = mysql_fetch_array($result);
  $margin = $row["margin"];
  $newprice = $record["PRICE"] * (1+($margin/100));
  mysql_query("UPDATE products SET price='".$newprice."' WHERE id='".$row["id"]."'");
}

Hope this helps!
Cheers,
David.