我已经为Magento2创建了一个插件,以删除“父类别路径”,但是却收到如下错误:
Fatal error: Uncaught Error: Class
'Myweb\RemoveParentCategoryPathPlugin\Plugin\Category' not found in
/public_html/app/code/Myweb/RemoveParentCategoryPathPlugin/Plugin/RemoveParentCategoryPathPlugin.php:8 Stack trace: #0
/public_html/vendor/magento/framework/Interception/Interceptor.php(135):
Myweb\RemoveParentCategoryPathPlugin\Plugin\RemoveParentCategoryPathPlugin->aroundGetUrlPath(Object(Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator\Interceptor),
Object(Closure), Object(MGS\Mpanel\Model\Category\Interceptor)) #1
/public_html/vendor/magento/framework/Interception/Interceptor.php(153):
Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator\Interceptor->Magento\Framework\Interception\{closure}
(Object(MGS\Mpanel\Model\Category\Interceptor)) #2
/public_html/generated/code/Magento/CatalogUrlRewrite/Model/CategoryUrlPathGenerator/Interceptor.php(26):
Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator\Interceptor->___cal in /public_html/app/code/Myweb/RemoveParentCategoryPathPlugin/Plugin/RemoveParentCategoryPathPlugin.php on line 8
我的插件模块文件如下:
app/code/Myweb/RemoveParentCategoryPathPlugin/Plugin/RemoveParentCategoryPathPlugin.php
<?php
namespace Myweb\RemoveParentCategoryPathPlugin\Plugin;
class RemoveParentCategoryPathPlugin
{
public function aroundGetUrlPath($subject, $proceed, $category)
{
if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
return '';
}
$path = $category->getUrlPath();
if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
return $path;
}
$path = $category->getUrlKey();
if ($path === false) {
return $category->getUrlPath();
}
return $path;
}
}
app/code/Myweb/RemoveParentCategoryPathPlugin/etc/di.xml
的内容是
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator">
<plugin name="Myweb_RemoveParentCategoryPathPlugin" type="Myweb\RemoveParentCategoryPathPlugin\Plugin\RemoveParentCategoryPathPlugin" />
</type>
</config>
app/code/Myweb/RemoveParentCategoryPathPlugin/etc/module.xml
的内容是
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Myweb_RemoveParentCategoryPathPlugin" setup_version="1.0.5" />
</config>
app/code/Myweb/RemoveParentCategoryPathPlugin/registration.php
的内容是
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Myweb_RemoveParentCategoryPathPlugin',
__DIR__
);
我正在努力寻找我错过的东西。
答案 0 :(得分:2)
在您的插件类RemoveParentCategoryPathPlugin中,类别类丢失。 在文件中添加“使用Magento \ Catalog \ Model \ Category”这一行。