Magento 2 Fetch all Categories and Sub Categories

ini_set('display_errors', 1);
require __DIR__ . '/app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');

// used for updating product stock – and it’s important that it’s inside the while loop
$stockRegistry = $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface');

// get list of all the categories
$categories = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory')->create();
$categories->addAttributeToSelect('*');

foreach ($categories as $category) {
echo 'Catgeory Id: ' . $category->getId();
echo 'Catgeory Name: ' . $category->getName();
echo 'Catgeory Url: ' . $category->getUrl();
echo '';
}

// Get current store categories

$categoryHelper = $objectManager->get('\Magento\Catalog\Helper\Category');
$categories = $categoryHelper->getStoreCategories();

foreach ($categories as $category) {
echo 'Catgeory Id: ' . $category->getId();
echo 'Catgeory Name: ' . $category->getName();
echo '';
}

 

Magento 2 Fetch all Categories and Sub Categories

How to delete images from any products programmatically in magento 2

How to install Themes in Magento 2

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top