微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 URL Key 字段中指定的值将生成一个已经存在的 URL

如何解决在 URL Key 字段中指定的值将生成一个已经存在的 URL

我正在尝试在 Magento 2.3 中保存和复制产品,但收到以下消息:

在 URL Key 字段中指定的值会生成一个已经存在的 URL。 要解决此冲突,您可以将 URL Key 字段(位于搜索引擎优化部分)的值更改为唯一值,或更改下面列出的所有位置的请求路径字段:

我尝试创建自定义模块来覆盖 DbStorage.PHP 文件的 doReplace 函数
论文是文件 模块.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="Urls_FixUrls" setup_version="1.1.0">
    </module>
</config>

注册.PHP

    <?PHP \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,'Urls_FixUrls',__DIR__
);

di.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\UrlRewrite\Model\Storage\DbStorage"
                type="Urls\FixUrls\Model\DbStorage" />
</config>

DbStorage.PHP

    <?PHP
namespace Urls\FixUrls\Model;

class DbStorage extends Magento\UrlRewrite\Model\Storage\DbStorage
{
    protected function doReplace($urls)
    {
        foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
            $urlData[UrlRewrite::ENTITY_TYPE] = $type;
            $this->deleteByData($urlData);
        }
        $data = [];
        $storeId_requestPaths = [];
        foreach ($urls as $url) {
            $storeId = $url->getStoreId();
            $requestPath = $url->getRequestPath();
            // Skip if is exist in the database
            $sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
            $exists = $this->connection->fetchOne($sql);

            if ($exists) continue;

            $storeId_requestPaths[] = $storeId . '-' . $requestPath;
            $data[] = $url->toArray();
        }

        // Remove duplication data;
        $n = count($storeId_requestPaths);
        for ($i = 0; $i < $n - 1; $i++) {
            for ($j = $i + 1; $j < $n; $j++) {
                if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
                    unset($data[$j]);
                }
            }
        }
        $this->insertMultiple($data);
    }
        
}

安装升级和缓存清理后,我尝试访问任何产品,它给我消息说
ma​​gento.local 目前无法处理此请求。 HTTP 错误 500

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。