0

Shopkeeper created configurable products by duplicating existing products and did not change the product URLs to match the product names. The shop is also a multilingual shop and different languages are made as store views.

This all lead to situation where the duplicated products had wrong url keys.

I found this code that could be added to a new .php file and run on the server and that was a solution for ONE store view:

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

That code changed the product url keys to match the product names, but it only worked for one store view.

My question is: how do I change the product url keys for the other store views in other languages?

UPDATE! The solution was easy: in the frontend just change to the next store view and run the new .php file in that view also.

3 Answers 3

2

The reason why that worked, is because Magento internals will use the current store id on Mage::getModel('catalog/product').

You could have used Mage::getModel('catalog/product')->setStoreId('STORE_ID');

1

Here is the improved code from the original question and taking into account the comments from @Tim Bezhashvyly regarding multiple store views.

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

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

$allStores = Mage::app()->getStores();
foreach ($allStores as $_eachStoreId) { 
    $_storeId = Mage::app()->getStore($_eachStoreId)->getId();  
    $amount = 0;
    $model = Mage::getModel('catalog/product')->setStoreId($_storeId);
    products = $model->getCollection();

    foreach ($products as $product) {
        $model->load($product->getId());
        $product->setUrlKey($model->getName())->save();
        set_time_limit(0);
        $amount++;
    }
}
0

You can also loop through your store views using Mage_Core_Model_App::getStores method.

2
  • How would you apply this modification to the php code in the question?
    – Webninja
    May 7, 2014 at 10:42
  • Come on. It's easy. Get store views collection, inside of your products collection loop lop through store views collection and do your routine inside. May 7, 2014 at 12:29

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.