I have a script which reads my Magento products and then creates a feed, which at the moment is only used for Googlebase. Becuase of the amount of products this script pages through them 100 at a time and to do this creates a small file on the server (google_base_reload.txt) which keeps the last number.
At the moment this file is not deleted at the end of the process so I cannot automate it as I have to manually delete it. Could I add something to the end of the following script to delete google_base_reload.txt once it's complete?
<?php
define('SAVE_FEED_LOCATION','export/google_base_feed.txt');//you can set a new folder and file if you want, don't forget to chmod the folder to 777
define('SAVE_FEED_RELOAD','export/google_base_reload.txt');//you can set a new folder and file if you want, don't forget to chmod the folder to 777
// Set the VAT rate as Google Base requires prices including VAT and we show them on the site excluding VAT. Rate set as a decimal not percentage
$vat=1.175;
// make sure we don't time out
set_time_limit(0);
require_once 'app/Mage.php';
Mage::app('default');
try{
if (file_exists(SAVE_FEED_RELOAD)) {$reload_line=file_get_contents(SAVE_FEED_RELOAD);} else {$reload_line=0;}
if ($reload_line==0){$handle = fopen(SAVE_FEED_LOCATION, 'w');} else {$handle = fopen(SAVE_FEED_LOCATION, 'a+');}
$handle_reload = fopen(SAVE_FEED_RELOAD, 'w');
if ($reload_line==0){
$heading = array('id','title','description','link','image_link','price','product_type','condition', 'c:product_code');
$feed_line=implode("\t", $heading)."\r\n";
fwrite($handle, $feed_line);
}
//---------------------- GET THE PRODUCTS
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', 1);//enabled
$products->addAttributeToFilter('visibility', 4);//catalog, search
$products->addAttributeToSelect('*');
$prodIds=$products->getAllIds();
//echo 'Product filter: '.memory_get_usage(false).'<br>';
//flush();
//$i=0;
foreach($prodIds as $productId) {
if (($productId>=$reload_line) & ($productId<$reload_line+100)){
$product = Mage::getModel('catalog/product');
//echo '. ';
//flush();
//echo 'Loop start: '.memory_get_usage(false).'<br>';
//flush();
//$product = Mage::getModel('catalog/product');
$product->load($productId);
$product_data = array();
$product_data['sku']=$product->getSku();
$product_data['title']=$product->getName();
$product_data['description']=$product->getShortDescription();
$product_data['link']=$product->getProductUrl(). '?source=googleps';
$product_data['image_link']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
// Get price of item
if($product->getSpecialPrice())
$product_data['price']=$product->getSpecialPrice();
else
$product_data['price']=$product->getPrice();
// Check if item subject to VAT and add on for Google requirements
$tax_class=$product->getTaxClassId();
/* Check tax code and add vat if necessary */
if ($tax_class=='2') {
$product_data['price'] = str_replace('"', '', $product_data['price']) * $vat;
}
unset($tax_class);
// Round price to 2 decimal places
$product_data['price']=round($product_data['price'], 2);
//$product_data['brand']=$product->getResource()->getAttribute('manufacturer')->getFrontend()->getValue($product);
$product_data['product_type']='';
$product_data['condition']='new';
$product_data['c:product_code']=$product->getSku();
//echo 'Product load: '.memory_get_usage(false).'<br>';
//flush();
//get the product categories
foreach($product->getCategoryIds() as $_categoryId){
$category = Mage::getModel('catalog/category')->load($_categoryId);
$product_data['product_type'].=$category->getName().', ';
}
$product_data['product_type']=rtrim($product_data['product_type'],', ');
//echo 'Category load: '.(memory_get_usage(false)).'<br>';
//sanitize data
foreach($product_data as $k=>$val){
$bad=array('"',"\r\n","\n","\r","\t");
$good=array(""," "," "," ","");
$product_data[$k] = '"'.str_replace($bad,$good,$val).'"';
}
$feed_line = implode("\t", $product_data)."\r\n";
fwrite($handle, $feed_line);
fflush($handle);
//echo 'Loop end: '.memory_get_usage(false).'<br>';
//flush();
}
}
//---------------------- WRITE THE FEED
fclose($handle);
$feed_line_reload=$reload_line+100;
echo "Processing from: ". $feed_line_reload . " to " . ($feed_line_reload + 99) ." product ids...";
fwrite($handle_reload, $feed_line_reload);
fclose($handle_reload);
}
catch(Exception $e){
die($e->getMessage());
}
if (end($prodIds)<$reload_line){echo "<br>The Google Base feed has been created.";}
else {
echo '<META http-equiv="refresh" content="1;URL=gbase.php">';
}
?>