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
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'; } ?>
No comments:
Post a Comment