Clearing URL keys in Magento

10,166

Solution 1

Here's a quickie that isn't even tested. It might take a long time if there are many products but it will also update the rewrite records at the same time. Copy this into a .php file in your site's root and execute it.

<?php
require 'app/Mage.php';
Mage::app();

$products = Mage::getModel('catalog/product')->getCollection();
foreach ($products as $product) {
    $product->setUrlKey($product->getSku())
            ->save();
}

Solution 2

Finally fixed it with the following code, building on the exemplae of clockworkgeek. Thanks for that!

<?php
require 'app/Mage.php';
Mage::app();
$amount = 0;
$model = Mage::getModel('catalog/product');
$products = $model->getCollection();
foreach ($products as $product) {
    $model->load($product->getId());
    $product->setUrlKey($model->getName())->save();
    set_time_limit();
    $amount++;
}
Share:
10,166

Related videos on Youtube

bo-oz
Author by

bo-oz

Just another boring bio.

Updated on September 16, 2022

Comments

  • bo-oz
    bo-oz over 1 year

    I've setup a magento installation for a shopowner who has added his own products. Unfortunataly he didn't understand the URl key field. As he duplicated the product, each product now has the same URL with a incrementing number /product-1234.html, next one /product-1235.html. Since he has almost 2k products, it will be a hassle to manually adjust all the url key's. Is there some way to clear this in magento (or straight on the DB) without ruining the shop. It seems that if I delete one URL-key magento auto-generates one, which is fine for me.

    Edit: Okay, so I've found how I can reset the URL key's by clearing certain fields in a database table (catalog_product_entity_varchar), but now I need Magento to create new ones using the product name. Any ideas?

    Thanks.

    • Peter O'Callaghan
      Peter O'Callaghan over 11 years
      If the URL key exists, if you removed it it'll use the name. Check out Mage_Catalog_Model_Url::_refreshProductRewrite.