skip to Main Content

Hi how can I retrieve the value of a category url in the backend? I’m expecting the url value to be equivalent to the url in the frontend and NOT the url in the backend. Is there a way to achieve this in Magento 2?

2

Answers


  1. Chosen as BEST ANSWER

    Ok I found the answer for this. The solution is to use an App/Emulator that is available in Magento. The idea is to start the emulation before you execute the Frontend Catgory URL retrieval that you want and close the emulation when its done.

    Here are the links on how to implement it App Emulation. Didn't know it was this simple to solve the issue. Below is how my code looks like

    class MenuCategory extends MagentoFrameworkModelAbstractModel implements MagentoFrameworkDataObjectIdentityInterface
    {
    
        public function getStoreCategories($storeManager, $emulator)
        {
    
            $objectManager = MagentoFrameworkAppObjectManager::getInstance();
            $emulator->startEnvironmentEmulation(null, MagentoFrameworkAppArea::AREA_FRONTEND, true);
    
            $categoryFactory = $objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
            $categories = $categoryFactory->create()                              
                ->addAttributeToSelect('*')
                ->setStore($storeManager->getStore())
                ->addAttributeToFilter('level', array('eq' => 2))
                ->addIsActiveFilter()
                ->addAttributeToSort('position', 'asc'); 
    
            foreach ($categories as $category) {    
                echo $category->getUrl() . " - " . $category->getUrl() . "n";
    
            }
    
            $emulator->stopEnvironmentEmulation();
            return $content;
        }
    }
    

    So the idea here is to instantiate the emulator and make magento think that you are going to modify or perform like you are in the frontend hence the code MagentoFrameworkAppArea::AREA_FRONTEND when you close the environment emulation it will go back to how it was whether you are in adminhtml or frontend


  2. In order to get the category url you need to use the MagentoCatalogModelCategoryRepository function getUrl() like so:

    $objectManager = MagentoFrameworkAppObjectManager::getInstance();
    $emulator->startEnvironmentEmulation(null, MagentoFrameworkAppArea::AREA_FRONTEND, true);
    
    $categoryFactory = $objectManager->create('MagentoCatalogModelResourceModelCategoryCollectionFactory');
    $categories = $categoryFactory->create()                              
        ->addAttributeToSelect('*')
        ->setStore($storeManager->getStore())
        ->addAttributeToFilter('level', array('eq' => 2))
        ->addIsActiveFilter()
        ->addAttributeToSort('position', 'asc'); 
    
    foreach ($categories as $category) {    
        $categoryObject = $objectManager->create('MagentoCatalogModelCategoryRepository')->get($category->getId());
        echo $categoryObject->getUrl()."n";
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search