【问题标题】:Display Frontend Category URL in Backend Magento在后端 Magento 中显示前端类别 URL
【发布时间】:2019-02-12 09:58:46
【问题描述】:

您好,我如何在后端检索类别 url 的值?我期望 url 值等于前端中的 url 而不是后端中的 url。有没有办法在 Magento 2 中实现这一点?

【问题讨论】:

    标签: magento magento2 magento-2.0 magento2.2 magento2.1


    【解决方案1】:

    好的,我找到了答案。解决方案是使用 Magento 中提供的 App/Emulator。我们的想法是在执行所需的前端类别 URL 检索之前启动模拟,并在完成后关闭模拟。

    这里是有关如何实现它的链接App Emulation。不知道解决问题这么简单。下面是我的代码的样子

    class MenuCategory extends \Magento\Framework\Model\AbstractModel implements \Magento\Framework\DataObject\IdentityInterface
    {
    
        public function getStoreCategories($storeManager, $emulator)
        {
    
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $emulator->startEnvironmentEmulation(null, \Magento\Framework\App\Area::AREA_FRONTEND, true);
    
            $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
            $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;
        }
    }
    

    所以这里的想法是实例化模拟器并使 magento 认为您将像在前端一样进行修改或执行,因此代码 \Magento\Framework\App\Area::AREA_FRONTEND 当您关闭环境仿真时,无论您是在 adminhtml 还是前端,它都会回到原来的状态

    【讨论】:

      【解决方案2】:

      为了获取类别网址,您需要使用Magento\Catalog\Model\CategoryRepository 函数getUrl(),如下所示:

      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      $emulator->startEnvironmentEmulation(null, \Magento\Framework\App\Area::AREA_FRONTEND, true);
      
      $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
      $categories = $categoryFactory->create()                              
          ->addAttributeToSelect('*')
          ->setStore($storeManager->getStore())
          ->addAttributeToFilter('level', array('eq' => 2))
          ->addIsActiveFilter()
          ->addAttributeToSort('position', 'asc'); 
      
      foreach ($categories as $category) {    
          $categoryObject = $objectManager->create('Magento\Catalog\Model\CategoryRepository')->get($category->getId());
          echo $categoryObject->getUrl()."\n";
      }
      

      【讨论】:

      • 已经尝试过了,这将根据视图显示类别 url。如果它在 storeview 中,它将生成一个 url 商店类别 url。但是当在管理员时,它会为管理员生成一个类别 url
      • 最上面的是我正在使用的代码。我将代码放在我的模型库中
      • 你正在使用 Magento\Catalog\Model\ResourceModel\Category\CollectionFactory 类,它是一个集合类,你无法获取 URL,你必须使用 Magento\Catalog\Model\CategoryRepository 加载单个类别,然后你会得到类别 URL。
      • 我不确定您的意思,但我能够使用此代码检索所有类别。该集合将返回多个类别。然后我遍历所有可用的类别并显示它
      • 我已经更新了我的代码,你可以在你的函数中使用它。
      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多