Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing URL keys in Magento

Tags:

magento

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.

like image 457
bo-oz Avatar asked Dec 15 '22 16:12

bo-oz


2 Answers

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();
}
like image 52
clockworkgeek Avatar answered Jan 01 '23 22:01

clockworkgeek


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++;
}
like image 23
bo-oz Avatar answered Jan 02 '23 00:01

bo-oz