Magento Coding Solutions

Magento category programmatically tree view



$cat_mod = Mage::getModel('catalog/category')->load(2);
    $helper = Mage::helper('catalog/category');
    $_main_categories=$helper->getStoreCategories();
    echo cateList($_main_categories,0);
   
    function cateList($_main_categories,$out){
        $i=0;
           
        $cathtml='';
        //print_r($_main_categories);
        $lable='';
        foreach($_main_categories as $_category){
             if ($_category->getIsActive()){
               
                while($i<$out){
                    $space .= '   ';
                    $i++;
                }
                ?>
               
                               
                $children = $_category->getChildren();
       
                $childrencount = $children->count();
                $hasChildren = $children && $children->count();
                if($hasChildren && $children->count())
                {
                    //echo "--->";
                    $out++;
                    cateList ($children,$out);
                    $out--;
                }
            }
           
           
           
   


























  
}
  


























 






















































      
        //return "df".$cathtml;
   
    }

?>

Magento add category programmatically


require_once 'app/Mage.php';
Mage::app('default'); // Default or your store view name.
//get a new category object
$category = Mage::getModel('catalog/category');
$category->setStoreId(0); // 0 = default/all store view. If you want to save data for a specific store view, replace 0 by Mage::app()->getStore()->getId().
//if update
if ($id) {
  $category->load($id);
}
$general['name'] = "My Category";
$general['path'] = "1/2/23"; // catalog path
$general['description'] = "Great My Category";
$general['meta_title'] = "My Category"; //Page title
$general['meta_keywords'] = "My , Category";
$general['meta_description'] = "Some description to be found by meta search robots. 2";
$general['landing_page'] = ""; //has to be created in advance, here comes id
$general['display_mode'] = "PRODUCTS_AND_PAGE"; //static block and the products are shown on the page
$general['is_active'] = 1;
$general['is_anchor'] = 0;
$general['page_layout'] = 'two_columns_left';
//$general['url_key'] = "cars";//url to be used for this category's page by magento.
//$general['image'] = "cars.jpg";
$category->addData($general);
try {
    $category->save();
    echo "Success! Id: ".$category->getId();
}
catch (Exception $e){
    echo $e->getMessage();
}