1

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?

1
<?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

  • could you pls explain brief – zus Jan 24 '18 at 5:44
  • FYI : snag.gy/6LdTjV.jpg – zus Jan 24 '18 at 5:47
  • I have use 2 which is Default Root Category (ID: 2), You can use according to your root Category – Rahul Singh Jan 24 '18 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 – Rahul Singh Jan 24 '18 at 5:49
  • Is my code right : justpaste.it/1g4bi – zus Jan 24 '18 at 5:55
0

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);
?>
  • where will the output save? – zus Jan 24 '18 at 6:43
  • just print_r($sub_categories) you will get all ids. – stalinrajindian Jan 24 '18 at 6:49
  • is possible to download as CSV file in output folder. – zus Jan 24 '18 at 6:51
  • yes. now I have updated the code. – stalinrajindian Jan 24 '18 at 6:53
  • now the output look like : 234567 how can i separate 2,3,4,5,6,7 – zus Jan 24 '18 at 6:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.