// 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);
Magento Code Snippet
Magento Solutions: Magento Code Snippet, Magento Tips
Saturday 16 July 2011
Magento Load Product By Name / SKU / ID
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
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 ?>
Saturday 9 July 2011
Magento 'Shop By Brand' in SideBar
First create the template file and name it productbrand.phtml place it in catalog/product folder
copy the code given below and paster it in you productbrand.phtml
Note: Please chnage 'yourdomain.com' with your site domain name
<div>
<div>
<h4>
<span>Product Brands</span></h4>
</div>
<div style="padding-left:8px;">
<?php
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($product->getResource()->getTypeId())->addFieldToFilter('attribute_code', 'manufacturer');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$manufacturers = $attribute->getSource()->getAllOptions(false);
?>
<ul id="manufacturer_list"><?php foreach ($manufacturers as $manufacturer): ?>
<li><a href="http://www.yourdomain.com/catalogsearch/advanced/result/?manufacturer[]=<?php echo $manufacturer['value'] ?>"><?php echo $manufacturer['label'] ?></a>
</li>
<?php endforeach; ?> </ul>
</div>
<div>
</div>
</div>
copy the code given below and paster it in you productbrand.phtml
Note: Please chnage 'yourdomain.com' with your site domain name
<div>
<div>
<h4>
<span>Product Brands</span></h4>
</div>
<div style="padding-left:8px;">
<?php
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter($product->getResource()->getTypeId())->addFieldToFilter('attribute_code', 'manufacturer');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$manufacturers = $attribute->getSource()->getAllOptions(false);
?>
<ul id="manufacturer_list"><?php foreach ($manufacturers as $manufacturer): ?>
<li><a href="http://www.yourdomain.com/catalogsearch/advanced/result/?manufacturer[]=<?php echo $manufacturer['value'] ?>"><?php echo $manufacturer['label'] ?></a>
</li>
<?php endforeach; ?> </ul>
</div>
<div>
</div>
</div>
Friday 8 July 2011
Create Customer By Code In Magento
Creating customer programmaticaly is easy just use the code below.
$websiteId = Mage::app()->getWebsite()->getId(); $store = Mage::app()->getStore(); //$customer = new Mage_Customer_Model_Customer(); $customer = Mage::getModel("customer/customer"); $customer->website_id = $websiteId; $customer->setStore($store); $customer->firstname = "vikram"; $customer->lastname = "singh"; $customer->email = "singh@yourdomain.com"; $customer->password_hash = md5("password"); $customer->save();
Wednesday 6 July 2011
Change Admin URL In Magento
Its easy to change admin URL in Magneto, open local.xml at location below,
/app/etc/local.xmlnow search for the code below
now replace 'admin' with your desired name.
as you do so your admin URL will be changed to adminarea
Magento Display Category BestSellers
$curr_categoryid = Mage::registry('current_category')->getId(); $cat_Id= $curr_categoryid; $category = Mage::getModel('catalog/category')->load($cat_Id); $products = Mage::getResourceModel('reports/product_collection') ->addOrderedQty() ->addAttributeToSelect('*') ->setOrder('ordered_qty', 'desc') ->addCategoryFilter($category);
Subscribe to:
Posts (Atom)