gpt4 book ai didi

php - 在 magento 2 中重新生成 URL

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:37:02 25 4
gpt4 key购买 nike

我在 Magento 2 中开发我的网站,我需要在更改类别 URL 后为产品重新生成 URL。

现有产品的 URL 没有改变。

例如:

类别网址:新网址: http://test.com/jewellery-collections/gold-jewellery/gold-rings旧网址: http://test.com/collections/gold/rings

产品网址: http://test.com/collections/gold/rings/midas-bloom

但网址应该是 http://test.com/jewellery-collections/gold-jewellery/gold-ringsmidas-bloom

因此,应根据新类别 URL 重新生成产品 URL

最佳答案

一个非常简单的解决方案是使用一个准备运行的 Magento 2 模块,它完全可以完成重写索引器的工作。您可以通过代码或从命令行调用此重新生成。更好的解决方案是通过您自己的导入器模块中的代码来完成。再生很简单

foreach($list as $product) {
if($store_id === Store::DEFAULT_STORE_ID)
$product->setStoreId($store_id);
$this->urlPersist->deleteByData([
UrlRewrite::ENTITY_ID => $product->getId(),
UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE,
UrlRewrite::REDIRECT_TYPE => 0,
UrlRewrite::STORE_ID => $store_id
]);
try {
$this->urlPersist->replace(
$this->productUrlRewriteGenerator->generate($product)
);
} catch(\Exception $e) {
$out->writeln('<error>Duplicated url for '. $product->getId() .'</error>');
} }

如您所见,Magento 2 为您提供了一个用于重新生成 url 重写\Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator (productUrlRewriteGenerator) 的模型,您可以将其注入(inject)到您的模型中。这个生成的 url 重写可以被\Magento\UrlRewrite\Model\UrlPersistInterface (urlPersist) 代替。您可以在导入后对所有产品或更改后的每个产品循环运行此操作。

或者您可以查看 https://github.com/Iazel/magento2-regenurl

关于php - 在 magento 2 中重新生成 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44897560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com