在后端 Magento 中显示前端类别 URL
Display Frontend Category URL in Backend Magento
您好,如何在后端检索类别 url 的值?我希望 url 值等同于前端中的 url 而不是后端中的 url 。有没有办法在 Magento 2 中实现这一点?
为了获得类别 url,您需要使用 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";
}
好的,我找到了答案。解决方案是使用 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 还是在前端,它都会回到原来的状态
您好,如何在后端检索类别 url 的值?我希望 url 值等同于前端中的 url 而不是后端中的 url 。有没有办法在 Magento 2 中实现这一点?
为了获得类别 url,您需要使用 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";
}
好的,我找到了答案。解决方案是使用 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 还是在前端,它都会回到原来的状态