0

How can I reset the product URL's so that it will automatically generate new ones based on the product name.

I tried this code inside a new .php file, but that did not work:

<?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++;
}?>
2

Please follow these steps Jelle.

  1. back up your database

I suppose that your url_key attribute code for product will have attribute_id = 97

You can check by run this query

SELECT attribute_id FROM `eav_attribute` where `attribute_code` = 'url_key' and `entity_type_id` = 4 
  1. run this query

DELETE FROM catalog_product_entity_varchar WHERE attribute_id = 97

  1. Reindex your product URL

Goto System -> Index Management

Reindex catalog URL rewrites

Hope it helps.

1

You need to use

 $model->setUrlKey($product->getName())->save();

Instead of

 $product->setUrlKey($model->getName())->save();

Also: add addAttributeToSelect('name') for add proudtc name at Collection $products = $model->getCollection()->addAttributeToSelect('name');

Change Mage::app(); to Mage::app('YOUR_STORE_CODE') or use Mage::app('admin')

Note: if you have multi store then you need to add store filter on that collection.

0

Try this:

<?php
require_once 'abstract.php';

class Fix_Url extends Mage_Shell_Abstract
{
    public function run() {
        $products = Mage::getModel('catalog/product')->getCollection();
        $products->addAttributeToSelect('name');

        foreach ($products as $product) {
            Mage::getSingleton('catalog/product_action')
                ->updateAttributes(array($product->getId()), array('url_key' => $product->getName()), 0);
        }
    }
}

$script = new Fix_Url();
$script->run();

Copy in shell subfolder (important!) and run it using:

php -q yourscriptname.php
php -q indexer reindexall

Hope will fix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.