gpt4 book ai didi

magento2 - Magento 2 - 以编程方式更改 url key

转载 作者:行者123 更新时间:2023-12-05 06:41:33 25 4
gpt4 key购买 nike

有没有办法为所有产品生成 URL key 并使用脚本保存它们?

我从数据库中删除了产品的所有 URL key ,但现在我想使用脚本重新生成它们。

//编辑:我需要在 Magento 2 中执行此操作。忘记指定。

我到现在才知道:

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);    
$obj = $bootstrap->getObjectManager();
$deploymentConfig = $obj->get('Magento\Framework\App\DeploymentConfig');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$repo = $objectManager->get('Magento\Catalog\Model\ProductRepository');

$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();

foreach ($collection as $product){
$name = $product->getName();
$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
$url = strtolower($url);
echo $url;
$pr = $repo->getById($product->getId());
$pr->setUrlKey($url);

$repo->save($pr);
break;
}

但是我得到这个错误:

Fatal error: Call to undefined function Magento\Catalog\Model\Config\Source\Product\Options__() in /home2/magazi70/public_html/vendor/magento/module-catalog/Model/Config/Source/Product/Options/Price.php on line 23

最佳答案

<?php
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$deploymentConfig = $obj->get('Magento\Framework\App\DeploymentConfig');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollection = $objectManager->create('\Magento\Catalog\Model\Product');

$collection = $productCollection->create()
->addAttributeToSelect('*')
->load();

foreach ($collection as $product){

$product = $objectManager->create('\Magento\Catalog\Model\Product')->load($product->getId());
$name = $product->getName();
$url = preg_replace('#[^0-9a-z]+#i', '-', $name);
$url = strtolower($url);

$product ->setUrlKey($url);
$product->save($pr);

}

关于magento2 - Magento 2 - 以编程方式更改 url key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40246652/

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