The Below code is tell you how to check a category exist in magento and this function will create Parent and sub category automatically in magento.
This function is first check category exist or not . if category does not exit than create a new category and return it’s category Id.
$cat_name = 'new category'; $p_cat_id = $this->createParentCategory($cat_name);
This function is first check child category exit on parent category or not. . if category does not exit than create a new category and return it’s category Id.
$child_cat_name = 'new child category'; $c_cat_id = $this->createParentCategory($child_cat_name,$p_cat_id);
protected function createParentCategory($cat_name,$parentId) { $categories = Mage::getResourceModel('catalog/category_collection'); $categories->addAttributeToFilter('is_active', 1) ->addAttributeToFilter('name', $cat_name) ->setCurPage(1)->setPageSize(1) ->load(); $pId = $categories->getData(); $pId = $pId[0]['entity_id']; if ($pId) { $pId = $pId; }else { $parentId = $parentId?$parentId:'2'; $category = new Mage_Catalog_Model_Category(); $category->setName($cat_name); //$category->setUrlKey('new-category'); $category->setIsActive(1); $category->setDisplayMode('PRODUCTS'); $category->setIsAnchor(0); $parentCategory = Mage::getModel('catalog/category')->load($parentId); $category->setPath($parentCategory->getPath()); $category->save(); unset($category); $pId = $this->getCategoryId($cat_name); } return $pId; } protected function getCategoryId($cat_name) { $categories = Mage::getResourceModel('catalog/category_collection'); $categories->addAttributeToFilter('is_active', 1) ->addAttributeToFilter('name', $cat_name) ->setCurPage(1)->setPageSize(1) ->load(); $pId = $categories->getData(); return $pId[0]['entity_id']; }
If you want delete all category in magento Click Here
If you want delete all Product in magento Click Here
Pingback: louis vuitton bags