I have three root category, i need all categories id for only selected root categories how can i get it. what are all the possible way to get it?
2 Answers
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 2;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>
<?php if (count($_categories) > 0): ?>
<ul>
<?php foreach($_categories as $_category): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php if($_category->hasChildren()):?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<ul>
<?php foreach($_subcategories as $_subcategory): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a>
<?php $_category2 = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
<?php if($_category2->hasChildren()):?>
<?php $_subcategories2 = $_category2->getChildrenCategories() ?>
<ul>
<?php foreach($_subcategories2 as $_subcategory2): ?>
<li>
<a href="<?php echo $_helper->getCategoryUrl($_subcategory2) ?>"><?php echo $_subcategory2->getName() ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Try this
-
-
-
I have use 2 which is Default Root Category (ID: 2), You can use according to your root Category Jan 24, 2018 at 5:48
-
FYI : snag.gy/6LdTjV.jpg – zus here you can use id:30 and it will show the sub categories associated with id 30 Jan 24, 2018 at 5:49
-
Please try below code. It's very simple.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
function convert_to_csv($input_array) {
/** open raw memory as file, no need for temp files */
$today = date('Ymd');
$file_path = "var/catgory_ids" . $today . ".csv";
$temp_memory_backup = fopen($file_path, 'w');
fwrite($temp_memory_backup, $input_array);
fclose($temp_memory_backup);
echo 'Data saved to csvfile.csv';
}
$catid = 2;
$sub_categories = Mage::getModel('catalog/category')->load($catid)->getChildrenCategories()->getAllIds();
$data = array();
$headers = array();
$header['cat_id'] = 1;
foreach ($sub_categories as $i => $category) {
$data[$i]['cat_id'] = $category;
}
$tmp_columns = array();
foreach ($header as $head => $v) {
$tmp_columns[] = $head;
}
$output.= implode(",", $tmp_columns) . "\n";
//building rows
foreach ($data as $rows) {
$output.= implode(",", $rows) . "\n";
}
// Send the CSV file to the browser for download
convert_to_csv($output);
?>