GitHub gist for creating new Magento Website scope
- Thursday, 09 May 2013
While working with Vm2Mage, we frequently test VirtueMart-to-Magento migrations with different VirtueMart setups, but we tend to use the same Magento instance over-and-over again. But for every VirtueMart-setup a new Magento Website-scope is created. Here's the script we use for that.
All source-code can be found in this Gist on GitHub. The source-code in this blog-post is limited to almost no code - and the code that is included is dummy code. However, the blog explains the why behind the actual Gist-code. I recommend you first check out the Gist itself, and then go back to this post for further details.
Auto-creating a Website, Store and Store View
The first thing to consider when creating a new Magento Website, is that a Website also needs a Store (technically referred to as a store-group) and a Store View (technically referred to as a store). Most commonly, if you create a new Website, you also need to create the other two. So our script does this.
$website = Mage::getModel('core/website')->setData($data)->save();
$group = Mage::getModel('core/store_group')->setData($data)->save();
$store = Mage::getModel('core/store_store')->setData($data)->save();
Unfortunately, the Store needs a reference to the Website (incremental ID) and the Store View needs a reference to the Store. So no matter in which order you create the three objects, when you reach the last object, you need to go back to the first two objects to update them. Our script therefor takes up the increment ID of each newly created object and reuses that ID for the update later on:
$groupId = $group->getGroupId();
A new Root Catalog
Additionally a new Root Catalog (so a category marked without parent) needs to be linked to the Store, so this is included in the Gist as well. For all naming the script just takes a basename (foobar) which is capatiized and/or suffixed with another word (Foobar Catalog). Once the Root Catalog is created, it can easily be linked to the Store.
Hope this code is useful to you as well.