🗂️ Magento 2 Categories & Subcategories – Organize Your Catalog Easily
In Magento 2, Categories and Subcategories help organize your products into a logical structure, making it easier for customers to browse and filter your store. Think of them like aisles and shelves in a supermarket.
📁 What is a Category?
A Category in Magento is a container for your products. You can create a hierarchy with subcategories to organize items by type, brand, or any other logical division. Each category can be set as:
- Anchor: Show layered navigation (filters)
- Include in Menu: Visible in the store’s top navigation
➕ How to Create Categories & Subcategories
- Go to Catalog > Categories
- Select Default Category or any parent
- Click Add Subcategory or Add Root Category
- Fill in the details:
- Name
- Is Active
- Include in Menu
- Save Category
📦 Assign Products to a Category
- Go to Catalog > Products
- Edit a product
- In the Categories section, click Assign Categories
- Select the desired category and save
💻 Create Category Programmatically
use Magento\Framework\App\Bootstrap;
use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Store\Model\StoreManagerInterface;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
/** @var CategoryFactory $categoryFactory */
$categoryFactory = $obj->get(CategoryFactory::class);
/** @var StoreManagerInterface $storeManager */
$storeManager = $obj->get(StoreManagerInterface::class);
$parentId = 2; // Default category root
$category = $categoryFactory->create();
$category->setName('Custom Category');
$category->setIsActive(true);
$category->setUrlKey('custom-category');
$category->setParentId($parentId);
$category->setStoreId($storeManager->getStore()->getId());
$category->setPath("1/$parentId");
$category->save();
🔗 SEO & Display Tips
- Use a clear URL Key like
mens-shoes - Add a Meta Title and Meta Description for SEO
- Upload a category image or CMS block for better visuals
🧠 Summary
- Categories organize products into a structured, navigable catalog
- Subcategories create deeper organization and filter options
- They improve SEO, navigation, and user experience
Now your store is ready to guide customers like a well-organized mall! 🏬✨