Magento: Get all category and subcategory products
Magento get all category and subcategory products which are assigned to categories at different levels. Below script will show you all the categories, and all the associated product names under each of those categories.
<?php | |
require_once('app/Mage.php'); | |
umask(0); | |
Mage::app('admin'); | |
set_time_limit(0); | |
$category = Mage::getModel('catalog/category'); | |
$tree = $category->getTreeModel(); | |
$tree->load(); | |
$ids = $tree->getCollection()->getAllIds(); | |
if ($ids) | |
{ | |
foreach ($ids as $id) | |
{ | |
$cat = Mage::getModel('catalog/category'); | |
$cat->load($id); | |
if($cat->getLevel()==3 && $cat->getIsActive()==1) | |
{ | |
$category1 = Mage::getModel('catalog/category')->load($cat->getId()); | |
$products = Mage::getResourceModel('catalog/product_collection') | |
->addAttributeToSelect('name') | |
->addCategoryFilter($category1); | |
echo "<b>".$cat->getName()."</b><br>"; | |
foreach ($products as $product) { //print_r($product->getData());exit; | |
echo " " . $product->getName() . " - ". $product->getSku() . "<br/>"; | |
} | |
} | |
} | |
} | |
?> |
Note the line which checks category getLevel()==3, you can change this line to get different subcategory levels by adjusting it.
For root category, getLevel() should be 1.
For all the main/primary categories, getLevel() should be 2.
For all the subcategories, getLevel() should be 3.
For all the subsubcategories, getLevel() should be 4.
and so on…
Above script will get you all the category and subcategories products, products assigned to each and every categories of your store.
Leave a comment
Welcome to my Blog
Certifications
Honor
Recognition
Contributions
Categories
- Apache (2)
- ChatGPT (1)
- Domain name (2)
- eCommerce (2)
- htaccess (1)
- Humor (3)
- Instagram API (1)
- jQuery (4)
- JSON (1)
- Linux (10)
- Magento (142)
- Magento admin (58)
- Magento Certification (5)
- Magento error (13)
- Magento frontend (68)
- Magento Imagine (2)
- Magento Interview (5)
- Magento Master (2)
- Magento2 (10)
- Mobile (1)
- MySQL (7)
- OpenAI (1)
- OroCRM (2)
- Performance (2)
- PHP (8)
- Prototype JS (3)
- Security (4)
- Wordpress (3)
- XML (2)
Tag Cloud
500 internal server error admin answers attribute bug category checkbox checkout cookie customer difference domain name EAV error event extension interview invoice jquery linux magento magento2 magento admin magento error magento interview questions magento orm mysql observer order pinterest product products questions redirect register remove script session simplexml to array state status study guide tax url wordpress