I am setting up a store that uses a drop ship supplier, meaning I don't have control of my stock. This means I need to keep the products on my site at least relatively up to date with their stock levels.
Fortunately, they provide an xml feed that has all the products listed along with the stock levels. I originally went about writing a script that simply looped through the feed and then updated the stock in product table - you can see a bit of it below:
Code: Select all
// get xml file contents
$xml = simplexml_load_file($url);
//update product db
foreach ($xml->products->product as $product) {
$query = "
UPDATE product
SET `quantity` = '$product->stock'
WHERE `product`.`sku` = '$product->code';
";
$result = mysql_unbuffered_query($query) or die ("Error in query: $query. ".mysql_error());
}
Does anyone with more experience than me (im a bit of a newbie to php!) have an idea of how I could go about updating my stock levels? I thought about doing it every time a product page is loaded, just for that product but not sure where to start really - I guess the xml file would need to be downloaded to speed up the user experience.
Any tips are much appreciated!