【问题标题】:Magento 2 - change url key programmaticallyMagento 2 - 以编程方式更改 url 键
【发布时间】:2016-10-25 18:00:22
【问题描述】:

有没有办法为所有产品生成 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;
}

但我收到此错误:

致命错误:在 /home2/magazi70/public_html/vendor/magento/module-catalog/Model/Config/Source/Product/Options 中调用未定义函数 Magento\Catalog\Model\Config\Source\Product\Options__() /Price.php 第 23 行

【问题讨论】:

  • 这段时间你的问题解决了吗?

标签: magento2


【解决方案1】:
<?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);

}

【讨论】:

  • 你能不能简单解释一下你解决了什么问题,以及你是如何解决的,而不是仅仅用一段代码来回答?这将有助于 OP 和其他任何人更好地了解您的解决方案。
【解决方案2】:

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

【讨论】:

  • 在这样做之后你还需要重新生成目录网址吗?
  • 可能是也可能不是。但根据问题,产品根本没有 url 键。
【解决方案3】:

加载一个集合并保存产品的新对象是完成这项工作的缓慢方法

这是最好的方法

composer require elgentos/regenerate-catalog-urls
php bin/magento module:enable Iazel_RegenProductUrl
php bin/magento setup:upgrade

更多信息请访问

https://github.com/elgentos/regenerate-catalog-urls

【讨论】:

  • 这将在 url_rewrite 表中创建 url 重写条目,但不会更新产品实体 url 键。
【解决方案4】:

这段代码展示了如何在帮助类中生成 url 键,与 Magento 2 在创建产品时生成 url 键的方式相同。 在示例中,我使用依赖注入以便在我的助手中使用 Magento\Catalog\Model\Product\Url 类。

namespace Myprojects\Mymodule\Helper;

use Magento\Catalog\Model\Product\Url;
use Magento\Framework\App\Helper\Context as HelperContext;


class Data extends AbstractHelper
{

    /**
    * @param Url $url
    */
    public function __construct(
                                HelperContext $context,
                                Url $url
                                )
    {
        parent::__construct($context);
        $this->url = $url;
    }

    public function generateUrlKey($string)
    {
        return $this->url->formatUrlKey($string);
    }

}

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 2012-11-22
    相关资源
    最近更新 更多