четверг, 6 сентября 2012 г.

How to create a new website/store/storeview in Magento (code snippet)

This is an example of install/upgrade script for creating a new website/store/storeview in Magento.
Also prefixes for order/invoice/shipment numbers are set.


<?php>
try {

    $websiteName = 'Website Name';
    $websiteCode = 'website_code';
    $storeName = 'Store Name';
    $storeViewName = 'Store View Name';
    //$rootCategoryName = 'Website Name Root Category';


    /** @var $installer Mage_Catalog_Model_Resource_Setup */
    $installer = $this;

    /** @var $write Zend_Db_Adapter_Abstract */
    $write = Mage::getSingleton('core/resource')->getConnection('core_write');

    $write->beginTransaction();

//    // Create root category
//    /**  @var Mage_Catalog_Model_Category $rootCategory */
//    $rootCategory = Mage::getModel('catalog/category');
//    $rootCategory->setName($rootCategoryName)
//        ->setDisplayMode('PRODUCTS')
//        ->setIsActive(1)
//        ->setPath('1')
//        ->setStoreId(0)
//    ;
//    //$rootCategory->setThumbnail(null);
//
//    $rootCategory->save();


    // Create website
    /** @var Mage_Core_Model_WebsiРte $website */
    $website = Mage::getModel('core/website');
    $website
        ->setName($websiteName)
        ->setCode($websiteCode)
        ->save();

    // Create Store
    /** @var Mage_Core_Model_Store_Group $store  */
    $store = Mage::getModel('core/store_group');
    $store
        ->setWebsiteId($website->getId())
        ->setName($storeName)
        // Error happened 'exception 'Exception' with message '$_FILES array is empty' in src/lib/Varien/File/Uploader.php:467' when this line is uncommented.
        //->setRootCategoryId($rootCategory->getId())
        ->save();

    // Set relations to Store in Website
    $website->setDefaultGroupId($store->getId());


    // Crate Store View
    /** @var Mage_Core_Model_Store $storeView  */
    $storeView = Mage::getModel('core/store');
    $storeView
        ->setName($storeViewName)
        ->setCode($websiteCode)
        ->setWebsiteId($website->getId())
        ->setGroupId($store->getId())
        ->setIsActive(1)
        ->save();

    // Set relations to StoreView in Store.
    $store->setDefaultStoreId($storeView->getId());
    /** @var $config Mage_Core_Model_Config */
    $config = Mage::getModel('core/config');

    // Set store name
    $config->saveConfig(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME, $websiteName, 'stores', $storeView->getId());


    // Add prefix for orders (5), invoices(6), shipments(8)
    $write->query("
        insert into `eav_entity_store` (entity_type_id, store_id, increment_prefix, increment_last_id)
        values
        (5, {$storeView->getId()}, 3, 300000000),
        (6, {$storeView->getId()}, 3, 300000000),
        (8, {$storeView->getId()}, 3, 300000000)
    ");

    $write->commit();

} catch (Exception $e) {
    $write->rollback();
    Mage::throwException($e);
}


Комментариев нет:

Отправить комментарий