Question

I need regenerate urls for all products (about 3000). Befere my regeneration urls are like https://my-shop.example.com/product/view/id/xxx/s/name-of-product. I need my products urls will be like https://my-shop.example.com/name-of-product.html.

I'm trying use magento method \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator->generate($product). It works but my diacritics (like ąęłóźż) are trimmed (e.g. product link become https://my-shop.example.com/jaki-tam-adny-produkt.html instead of https://my-shop.example.com/jakis-tam-ladny-produkt.html)

I tried use also $product->setUrlKey($generated_by_me_url_key_with_diacrtitics_replaced_by_english_characters) and $product->save(). Backend shows my changed url keys but I didn't seen changes on frontend. I tried use steps from https://github.com/magento/magento2/issues/5929#issuecomment-238170690 but it only restore ugly urls like was on start ( e.g. https://my-shop.example.com/product/view/id/xxx/s/name-of-product)

I'm using Magento 2.2.4

Was it helpful?

Solution

Finally, I resolved my problem - by doing:

  1. Set Use Default Store View for all product attributes including ULR keys as described here.
  2. Use 3 lines below on all products:

.

$product->setUrlKey($this->makeUrlKey($product->getName()));
$product->setUrlPath($this->makeUrlKey($product->getName()));
$product->save();

protected function makeUrlKey($str, $replace = array(), $delimiter = '-')
{
    setlocale(LC_ALL, 'en_US.UTF8');
    if (!empty($replace)) {
        $str = str_replace((array)$replace, ' ', $str);
    }

    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = strtolower(trim($clean, '-'));
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);

    return $clean;
}
  1. reindex, clear cache
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top