Search code examples
phpmagento2categories

Magento 2 get categories path names from product


I am trying to get for each product the category paths with names and exclude the root category(id 1). I separate them with

%%

I use this code:

<?php
$categories = $product->getCategoryIds();
$categoryNames = array();

foreach($categories as $category){
    $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
    $categoryNames[] = $cat->getPath();
}

$categoryList = implode(' %% ', $categoryNames);

echo $categoryList;

But instead of giving me the path names it give the category ids without removing root category:

1/3/1945 %% 1/3/1986/1995 %% 1/3/1986/1987 %% 1/3/1672 %% 1/3/2005/2032 %% 1/3/1672/1773/1774

I am not good at this. Any help please?


Solution

  • To get category name and remove the root catalog you should do it like that

    With code explanation below

    Category IDs Retrieval: The IDs of categories associated with the product are retrieved. Category Names and Paths: For each category: Its path is obtained and split into individual category IDs. Each category ID is loaded (if not already loaded) to get its name. Category names are accumulated to build the full path, excluding 'Root Catalog'. Output: All category paths are joined with %% and printed. The result is a string where each category path is displayed in a hierarchical manner, separated by %%.

    I hope it helps as all your requirements are added and one thing i remove root cat by name not by id, as both serve the purpose

            $categories = $product->getCategoryIds();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        
        $catNames = [];
        $processedCategories = [];
        $categoryPaths = [];
        
        foreach ($categories as $category) {
            if (in_array($category, $processedCategories)) {
                continue; // Skip already processed categories
            }
        
            $cat = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
            $pathParts = explode('/', $cat->getPath()); // Split the path into separate parts
            $categoryPath = [];
        
            foreach ($pathParts as $value) {
                if (!isset($catNames[$value])) { // Load category only if not already loaded
                    $categoryModel = $objectManager->create('\Magento\Catalog\Model\CategoryFactory')->create()->load($value);
                    $catNames[$value] = $categoryModel->getName();
                }
                if ($catNames[$value] !== 'Root CART') { // Avoid adding 'Root Catalog' to the path
                    $categoryPath[] = $catNames[$value];
                }
            }
        
            // Join the path with '>' and add it to the categoryPaths array
            $categoryPaths[] = implode('>', $categoryPath);
        
            $processedCategories = array_merge($processedCategories, $pathParts); // Mark categories as processed
        }
        
        // Join all category paths with '%%' and print
        echo implode(' %% ', $categoryPaths);
    

    enter image description here