I have created a script to update a custom attribute for all of my products and for some reason it isn't running. I have it located in the base folder of magento and I'm calling it using (base siteurl)/fitment.php and it's not executing. I've tested another script using echo and it works. Are there restrictions on reading a file from the base url or something? The file in question is below.
require_once('app/Mage.php');
umask(0);
Mage::app('default');
Mage::app ()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$path = Mage::getBaseDir('var') . '/import/YMM.csv';
$myfile = fopen($path, "r") or die(Mage::log("failed to open" . $path));
if ($myfile) {
$sku = array();
while (($line = fgets($myfile)) !== false) {
try {
//split file by comma
$array = explode(",", $line);
$count = 0;
$yearMakeModel = array();
$make = "";
$model = "";
foreach ($array as $ymm) {
if ($count == 1) {
$make = $ymm;
} elseif ($count == 2) {
$model = $ymm;
} elseif ($count > 1) {
array_push($yearMakeModel, $ymm);
}
$count++;
}
sort($yearMakeModel);
$fullText = $make . " " . $model . " ";
$count = 1;
$yearsText = "";
$multiple = false;
$set = false;
$previous = 0;
$lastElement = end($yearMakeModel);
foreach ($yearMakeModel as $years) {
if (!empty(trim($years))) {
if (!$set) {
$yearsText = $years;
$previous = $years;
$set = true;
} elseif ($previous + 1 == $years) {
if($years == $lastElement){
$yearsText = $yearsText . " - " . $years;
}
$previous = $years;
$multiple = true;
} else {
if ($multiple) {
$yearsText = $yearsText . " - " . $previous . ", " . $years;
$previous = $years;
$multiple = false;
} else {
$yearsText = $yearsText . ", " . $years;
$previous = $years;
$multiple = false;
}
}
}
$count++;
}
$fullText = $fullText . $yearsText;
if (array_key_exists($array[0], $sku)) {
$sku[$array[0]] = $fullText . "<br />" . $sku[$array[0]];
} else {
$sku[$array[0]] = $fullText;
}
} catch (Exception $e) {
Mage::log("error updating fitment attribute field");
Mage::log($e->getMessage());
}
}
/**
* insert fitment info into the fitment attribute
*/
try {
while ($year = current($sku)) {
echo key($sku) . "<br />";
$product = Mage::getModel('catalog/product');
$product = $product->load($product->getIdBySku(key($sku)));
$product = Mage::getModel('catalog/product')->load($product->getEntityId());
if($product->getId()){
$product->setData('fitment', $year)->getResource()->saveAttribute($product, 'fitment');
}
next($sku);
}
}catch(Exception $e){
Mage::log($e->getMessage());
}
}
Mage::app('default');
and see what happens. – Marius♦ Aug 19 '15 at 14:51