Saturday 16 July 2011

Magento Load Product By Name / SKU / ID

// Load by name
$_category = Mage::getModel('catalog/category')->loadByAttribute('name', 'Ruby Ring');
$_product = Mage::getModel('catalog/product')->loadByAttribute('name', 'emerald earring');

// Load by SKU
$_product = Mage::getModel('catalog/product')->loadByAttribute('sku', 'Eram18j4');

//Load by ID (just load):
$_product = Mage::getModel('catalog/product')->load($productID);

How To Upload File In Magento

if (isset($_FILES['logo']['name']) && $_FILES['logo']['name'] != '') {
    try {
        $uploader = new Varien_File_Uploader('logo');
        $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
        $uploader->setAllowRenameFiles(false);
        $uploader->setFilesDispersion(false);
        $path = Mage::getBaseDir('media') . DS;
        // $path = Mage::getBaseDir('media') . DS . 'logo' . DS;
        $logoName = $_FILES['logo']['name'];
        $uploader->save($path, $logoName);
  
    } catch (Exception $e) {
  
    }
}

Friday 15 July 2011

Magento Reindexing Problem Fixing programmability

When  ever  number of products  increased it get tuff to reindex all data it get more worse if you are on shared host. Now you can solve this issues with the code given below.

Create new file on your root i.e. reindex.php and copy all data given below and paste it in reindex.php
now you can start reindex data by just accessing URL like http://yourdomain.com/reindex.php

try{
 ob_implicit_flush(true);
 if(!ini_get('safe_mode')){set_time_limit(0);}
 ignore_user_abort(); 
 require_once("app/Mage.php");
 Mage::app('admin'); 
 Mage::setIsDeveloperMode(true);
 umask(0);
 
 
 $HM = 800; 
 $STOREID = '1'; 
 $HOWTORUN = 1; 
 
 
  $total = NULL;
  if($HOWTORUN == 1)
  {
  
   $prodCount = "cm_pd";
   if((file_exists($prodCount))&&(filesize($prodCount)>1)){
$fh = fopen($prodCount, 'r');
$total = fread($fh,filesize($prodCount)) + 1;
fclose($fh);
}
else
{
$fh = fopen($prodCount, 'w') or die("can't open file");

$tmpIDs = Mage::getModel('catalog/product')->getCollection()->getAllIds();

$total = $tmpIDs[sizeof($tmpIDs)-1]; //-1 Accounts for 0 in array.

fwrite($fh, $total);
fclose($fh);
}

}
else
{

if((!isset($_SESSION['pcount']))||($_SESSION['pcount']==NULL)){ 
$tmpIDs = Mage::getModel('catalog/product')->getCollection()->getAllIds();
$endValue[sizeof($tmpIDs)-1]; 
$_SESSION['pcount'] = $endValue;
$total = $_SESSION['pcount'];
}
else{ $total = $_SESSION['pcount']; }

}
if($total == NULL){ throw new Exception("Total Wasn't Captured"); }


echo("\n$total\n");

$countFile = "cm_cnt";
$count = null;
$store = Mage::app()->getStore($STOREID);


if((file_exists($countFile))&&(filesize($countFile)>1)){
$fh = fopen($countFile, 'r');
$count = fread($fh,filesize($countFile)) + 1;
fclose($fh);
if((int)$count == 0) $count = 1;
}
else
{
$fh = fopen($countFile, 'w') or die("can't open file");
$count = 0;
if((int)$count == 0) $count = 1;
fwrite($fh, $count);
fclose($fh);
}



if($count < 2)
 {
   Mage::app()->cleanCache();

$flag = Mage::getModel('catalogindex/catalog_index_flag')->loadSelf();
if ($flag->getState() == Mage_CatalogIndex_Model_Catalog_Index_Flag::STATE_RUNNING) $flag->delete();
}

$max = ($count+$HM);
$sx = Mage::app()->getStore('1');

ob_start();
for($i = $count; $i <= $max; $i++)
 {
 ob_end_flush();
 ob_start();
    if($i == $total)
    {
      echo "

Completed

"; @unlink($prodCount); @unlink($countFile); exit(1); } Mage::getSingleton('catalogindex/indexer')->plainReindex($i, null, $sx); echo("\n #Record $i#CURRENT MEMRY USAGE = ".memory_get_usage().";\n"); $fh = fopen($countFile, 'w') or die("can't open file"); fwrite($fh, $i); fclose($fh); ob_flush(); flush(); } }catch(Exception $e){ echo"

ERROR: $e

"; exit(1);} #EOF ?>