3
votes

Is there a way to generate URL Keys for all products and save them using a script?

I deleted all URL keys for products from database, but now I want to generate them again using a script.

// Edit: I need to do this in Magento 2. Forgot to specify.

I got this until now:

$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;
}

But I get this error:

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

4
Have you solved your problem in the meantime? - Atr_Max

4 Answers

2
votes
<?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);

}
0
votes

The magento script may take longer time.

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
-1
votes

to loading a collection and save the new object of product is slow way to done the job

here is best way to

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

more information are available on

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

-1
votes

This code shows how to generate an url key, in a helper class, the same way Magento 2 generates url keys when creating products. In the example I use dependency injection in order to use Magento\Catalog\Model\Product\Url class in my helper.

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);
    }

}