有没有办法为所有产品生成URL密钥并使用脚本保存?我从数据库中删除了所有产品的URL密钥,但现在我想使用脚本再次生成它们。
感谢。
//编辑:我需要在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
答案 0 :(得分:0)
<?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);
}
答案 1 :(得分:0)
magento脚本可能需要更长的时间。
1. You can try exporting the products (the csv file will not have url keys)
2. Remove all the attributes and keep only SKU and Name and add a new attribute column url_key
3. Use some Excel Functions to generate url keys using Name
4. Remove the Name column
5. Import the csv
答案 2 :(得分:-1)
加载集合并保存产品的新对象是完成工作的慢方法
这是前往
的最佳方法composer require elgentos/regenerate-catalog-urls
php bin/magento module:enable Iazel_RegenProductUrl
php bin/magento setup:upgrade
有关更多信息,请访问