Showing posts with label category id. Show all posts
Showing posts with label category id. Show all posts

Wednesday, October 5, 2011

Export Magento categories with ID using php script

Copy the code below into a new file "categorywithid.php" and place this file in your root folder
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
Mage::app();

$category = Mage::getModel ( 'catalog/category' );
$tree = $category->getTreeModel ();
$tree->load ();

$ids = $tree->getCollection ()->getAllIds ();

if ($ids) {
 $file = "var/export/catwithid.csv";
 file_put_contents($file,"catId, catName\n");
 foreach ( $ids as $id ) {
   $string = $id . ', ' .$category->load($id)->getName() . "\n";
  file_put_contents($file,$string,FILE_APPEND);
 }
}
Hit Your browser like www.YourDomain.com/categorywithid.php Thats it. You are done. Get the csv file under "var/export/catwithid.csv".

Wednesday, September 14, 2011

How to get Parent category id in Magento?

Here is the code to get parent category id.
$current_category = Mage::registry('current_category');
echo $parent_id = $current_category->parent_id; 

Magento how to get category id in product page?

<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
    <?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
    <a href="<?php echo $_category->getUrl() ?>"><?php echo $_category->getName() ?></a>
<?php endforeach; ?> 
This will load and link all of the categories that the product is found in.