1

I run below script in browser but I have more products and all finish in internal server error. Can I change URL for only configurable products? How can i re-generate URL key based on product name for each store view?

<?php  
    require 'app/Mage.php';
    Mage::app();
    Mage::getModel('catalog/product')->setStoreId(1);
    $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++;
    }

3 Answers 3

2

Try This code :-

<?php
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;

Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
ini_set('memory_limit', '600M');
ini_set('max_execution_time', 1800);
umask(0);
Mage::app('admin');

$products = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('type_id', 'configurable');
$productModel = Mage::getModel('catalog/product');

foreach ($products as $product) {
    $productNameLowercase = strtolower($product->getName());
    $productKey = clean($productNameLowercase);
    $productUpdate = $productModel->load($product->getId());
    $productUpdate->setUrlKey($productKey);
    $productUpdate->save();
}

function clean($string)
{
    $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

    return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
1
  • Getting Blank page
    – Zerocard
    Commented Sep 23, 2015 at 15:42
1

Your Script tries to load all the products in the store with ID=1 and change URL key and save, loading whole the product collection and save, in Magento creates a heavy load on server, so you get internal server error after a while and it's normal.

you can use extensions like http://www.magentocommerce.com/magento-connect/url-key-master-by-magendev.html which can re-generate URL key based on product name in each store view, but very quick and easy for massive number of products.

take a look at this video: http://magendev.com/urlkeymaster-doc/urlkeymaster-auto-generate.html

0

First of all, truncate all through server/phpmyadmin. For that,

SET FOREIGN_KEY_CHECKS = 0;
TRUNCATE TABLE core_url_rewrite;
TRUNCATE TABLE enterprise_catalog_category_rewrite;
TRUNCATE TABLE enterprise_catalog_product_rewrite;
TRUNCATE TABLE enterprise_url_rewrite;
TRUNCATE TABLE enterprise_url_rewrite_category_cl;
TRUNCATE TABLE enterprise_url_rewrite_product_cl;
TRUNCATE TABLE enterprise_url_rewrite_redirect_cl;
TRUNCATE TABLE enterprise_url_rewrite_redirect_rewrite;
TRUNCATE TABLE enterprise_url_rewrite_redirect;
SET FOREIGN_KEY_CHECKS = 1;

open your phpmyadmin and select your database and run above sql commands.

After this login to your admin panel and do the Reindex the data.

Now the problem will solve.

Please take the backup before do this. I hope it will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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