How to insert your block into any place in Magento

Hello guys, it’s time to tell you how to insert block into any place you want in Magento. As you know, very often we need to set some block into selected weird place without editing template, so we’ll try to describe the way how to do this using layouts and observers.In our case, for such experiments will be used category page. Let’s say, you need to place your block inside the other one and this block is instance of the Mage_Core_Block_Text_List class. That means, the block renders his children automatically, what’s easy. Then, you just need to follow the steps which are below:

    

This block will be displayed after all content of the “left“ block. But, what if we need it to be displayed before all content – just add the “before” directive. See below such example:

Important operatinol functions of Magento

1.How to get the category url in magento
<?php echo Mage::getModel('catalog/category')->load($catId)->getUrl() ?>
2.How to remove .html at the end of the url in magento
System => Configuration => Catalog => Search Engine Optimization and empty the fields "Product URL Suffix" and "Category URL Suffix"
3.How to get category’s Thumbnail image in magento
<?php $thumbnail=Mage::getModel('catalog/category')->load($categoryId)->getThumbnail();  //$category_Id -category id ,$thumbnail – image name
if($thumbnail !=''){
$imgPath = Mage::getBaseUrl('media').'catalog/category/'  // Get category image path
echo '';
?>
4.How to get Customer Details By Session
<?php  $customer = Mage::getSingleton('customer/session')->getCustomer();
    print_r($customer);?>
4.1How to get Customer Details By E-Mail
<?php   $customer = Mage::getModel('customer/customer')->loadByEmail(example@mail.com);
    print_r($customer); ?>
5.How to delete product by sku :-
<?php  $product = Mage::getModel('catalog/product');
   $productId = intval($product2->getIdBySku('putSkuHere');
   $product->load($productId)->delete(); ?>
6.How to get module table :-
<?php  
$resource = Mage::getSingleton('core/resource');
$tableName = $resource->getTableName('mymodule/mymodel') 
?>

how to upload any file in magento

Step 1 :- cretae html form for upload file
Step 2 :- Write Script for upload file
<?php

if(isset($_FILES['myfilename']['name']) && $_FILES['myfilename']['name'] != '')
{  
try
    {      
        $path = Mage::getBaseDir('media') . DS . 'csvfile' . DS;  //desitnation directory    
        $fname = $_FILES['myfilename']['name']; //file name
  $filpath = $path.$fname;
        $uploader = new Varien_File_Uploader('myfilename'); //load class
        $uploader->setAllowedExtensions(array('xls','xlsx','csv')); //Allowed extension for file
        $uploader->setAllowCreateFolders(true); //for creating the directory if not exists
        $uploader->setAllowRenameFiles(false); //if true, uploaded file's name will be changed, if file with the same name already exists directory.
        $uploader->setFilesDispersion(false);
        $uploader->save($path,$fname); //save the file on the specified path
    }
    catch (Exception $e)
    {
        echo 'Error Message: '.$e->getMessage();
    }
}
?>

How to create category from csv file

How to create bulk parent and sub category from csv file
Instruction:- Copy and save this code file in root folder of Magento and run it. exp :- http://yoursite/cat.php
<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
$app = Mage::app('admin'); 
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ini_set('max_execution_time', 1800);
umask(0);
/************ Get CSV file **************/
  $path = Mage::getBaseDir('media') . DS . 'catcsvfile'; //file path of csv
  $files = glob($path.'/*'); // get all file names
  if($files){
   foreach($files as $file){ // iterate files   
     $csv  =  new Varien_File_Csv();
     $data =  $csv->getData($file); //path to csv
        }  
 array_shift($data);
 foreach($data as $_data){
/*****************  Prent category creation *****************/
   /*****************  Prent category creation *****************/
   $Pcat =  utf8_encode($_data[0]); // column to create parent category
   $getcategory = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name',$Pcat);
   $catData = $getcategory->getData();
   $catcount  = count($catData);
    if($catcount==0 && !empty($Pcat))
   {  $category = Mage::getModel('catalog/category');
     $category->setName($Pcat);
     $category->setUrlKey($Pcat);
     $category->setMetaTitle($Pcat);
     $category->setIsActive(1);
     $category->setDisplayMode('PRODUCTS');
     $category->setIsAnchor(1); //for active achor
     $category->setStoreId(Mage::app()->getStore()->getId());
     $parentCategory = Mage::getModel('catalog/category')->load(2);
     $category->setPath($parentCategory->getPath());
     $category->save();
  }
/*    Child category creation             **********************/
   global $childcat;
    $childcat = utf8_encode($_data[1]); // column to create sub category
   $pgetcategory = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name',$childcat);
   $pcatData = $pgetcategory->getData();
   if(!$pcatData) {
    $getcategory2 = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name',$Pcat);
    $catData2 = array();
   $catData2 = $getcategory2->getData();
   if(!empty($catData2)){
   $ParentCategoryId2 = $catData2[0]['entity_id'];   
     $category = Mage::getModel('catalog/category');
     $category->setName($childcat);
     $category->setUrlKey($childcat);
     $category->setMetaTitle($childcat);
     $category->setIsActive(1);
     $category->setDisplayMode('PRODUCTS');
     $category->setIsAnchor(1); //for active achor
     $category->setStoreId(Mage::app()->getStore()->getId());
     $parentCategory = Mage::getModel('catalog/category')->load($ParentCategoryId2);
     $category->setPath($parentCategory->getPath());
     $category->save();
   }  
   }
 }
}
else
{
echo 'file not found';
}
?>

How to get category id by category name in magento

<?php
$category = Mage::getResourceModel('catalog/category_collection')->addFieldToFilter('name','cateNameHere'));
    $catData = $category->getData();      
 $categoryId = $catData[0][entity_id];
 echo $categoryId;
 ?>