Module Setup Script – How to Rename Website, Store Group, and Store

modulesetup-script

Is it possible to have a setup script do:

  1. Rename the default website
  2. Rename the default store group
  3. Rename the default store view
  4. Add another store view to a store group
  5. Add another store group to a website

We have vagrant boxes and when we on board a new developer we need these things to occur so that we don't have to create them manually.

This is what we've got so far:

Mage::register('isSecureArea', true);
// Remove default website, store, and store view
$defaultWebsiteModel = Mage::getModel('core/website');
$defaultStoreGroupModel = Mage::getModel('core/store_group');
$defaultStoreView = Mage::getModel('core/store');

Mage::unregister('isSecureArea');

Best Answer

Yes, websites, store groups and stores are models like any others and you can create and modify them in install scripts.

Here is an example that creates a new website with store group, root category and store:

//Switch to admin store (workaround to successfully save a category)
$originalStoreId = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

/** @var $rootCategory Mage_Catalog_Model_Category */
$rootCategory = Mage::getModel('catalog/category');
$rootCategory->setName('UK Root Category')
    ->setPath('1')
    ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
    ->save();

/** @var $website Mage_Core_Model_Website */
$website = Mage::getModel('core/website');
$website->setCode('uk')
    ->setName('UK Website')
    ->save();

/** @var $storeGroup Mage_Core_Model_Store_Group */
$storeGroup = Mage::getModel('core/store_group');
$storeGroup->setWebsiteId($website->getId())
    ->setName('Default')
    ->setRootCategoryId($rootCategory->getId())
    ->save();

/** @var $store Mage_Core_Model_Store */
$store = Mage::getModel('core/store');
$store->setCode('en_uk')
    ->setWebsiteId($storeGroup->getWebsiteId())
    ->setGroupId($storeGroup->getId())
    ->setName('UK - English')
    ->setIsActive(1)
    ->save();

//Set store to original value
Mage::app()->setCurrentStore($originalStoreId);

To rename an existing website, its default store group and store view, you can get it from the app model:

$ukWebsite = Mage::app()->getWebsite('main');
$ukWebsite->setName('New Website Name')->save();
$ukWebsite->getDefaultGroup()->setName('New Group Name')->save();
$ukWebsite->getDefaultStore()->setName('New Store Name')->save();

This should help you to do anything from your list.