Regenerate Product URLs for New Store – Magento 1.9 Guide

magento-1.9product-urlsurl

I have recently setup a new international store with english product titles, whereas my product titles are originally in danish. Now I need to regenerate all URLs for this store view, to use the english title instead of the old danish ones that are currently.

I order to do this, I guess I have to remove all existing URLs from this store and let magento create new ones. But what is the easiest way of doing this for around 2k products?

Thanks!

Best Answer

Here's a number of options:

  1. Update the URL keys the same way you updated the product titles.
  2. Export, transform values, import.
  3. Unset the URL keys programmatically and have them regenerated automatically.

While the former are rather self-explanatory, this is how the scripted approach looks like:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->setStoreId($newStoreId);
$collection->addStoreFilter($newStoreId);
$collection->addAttributeToSelect('*');

foreach ($collection as $product) {
    $product->setStoreId($newStoreId);

    // disable adding entries to url rewrite rules (optional):
    $product->setSaveRewritesHistory(false);

    // if there are already url keys on store scope:
    $product->unsUrlKey();

    // otherwise, if the new store uses default values from parent scope:
    $urlKey = $product->formatUrlKey($product->getName());
    $product->setUrlKey($urlKey);
}

$collection->save();

By default, URL rewrites are added to the system. You can optionally turn that behaviour off.

Unsetting the URL key will only have effect if the product already has a URL key in the new store scope. If the product pulls its URL key from a website or default scope, the new URL key must be set explicitly.