Magento – Programmatically Change All Categories for All Store Views

categorycollection;store-view

I want to change some data of all categories (all store views).

This is my code:

$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
asort($ids);
$categories = array();
if($ids) {
    foreach ( $ids as $id ) {
        $cat = $category->load($id);
        if($cat->getId()) {
            $cat->setName($cat->getName().' add some text');
            $cat->save();
        }
    }
}

Unfortunately I get only the data of the second language.
I need to change data of all languages (store views).

Does anyone has an idea?

Best Answer

$storeIds = Mage::getModel('core/store')->getCollection()->getAllIds();
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
asort($ids);
$categories = array();
if($ids) {
    foreach ( $ids as $id ) {
        foreach ($storeIds as $storeId) {
            if ($storeId == 0) {
                continue;
            }
            $cat = $category->setStoreId($storeId)->load($id);
            if($cat->getId()) {
                $cat->setName($cat->getName().' add some text');
                $cat->save();
            }
        }
    }
}
Related Topic