Magento 2 Categories & Subcategories

🗂️ 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

  1. Go to Catalog > Categories
  2. Select Default Category or any parent
  3. Click Add Subcategory or Add Root Category
  4. Fill in the details:
    • Name
    • Is Active
    • Include in Menu
  5. Save Category

📦 Assign Products to a Category

  1. Go to Catalog > Products
  2. Edit a product
  3. In the Categories section, click Assign Categories
  4. 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();

Try It Now

🔗 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! 🏬✨