Der folgende Script zeigt wie automatisiert über ein PHP-Setup-Script in Magento eine Category angelegt werden kann, welche dann als Default-Category einer neuen Store-Group zugewiesen wird, welche samt einer neuen Store-View angelegt wird.
$installer = $this;
$installer->startSetup();
$data = array(
'name' => 'New Category',
'url_key' => 'root', //'de',
'description' => 'test category',
'meta_description' => 'test category',
'meta_keywords' => 'categories_meta',
'display_mode' => Mage_Catalog_Model_Category::DM_PRODUCT,
'default_sort_by' => Mage::getModel("catalog/category")->getDefaultSortBy(),
'available_sort_by' => Mage::getModel("catalog/category")->getDefaultSortBy(),
'is_active' => 1,
'is_anchor' => 0,
'include_in_menu' => 0,
'parent_id' => 1,
'path' => 1, //Mage::getModel("catalog/category")->load(1)->getPath(),
'attribute_set_id' => Mage::getModel("catalog/category")->getDefaultAttributeSetId(),
'display_mode' => array( '0' => 'show_products'),
'custom_design_apply' => 1
);
Mage::app()->getStore()->load(0);
$cat = Mage::getModel('catalog/category');
$cat->addData($data);
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
$parentCategory = Mage::getModel('catalog/category')->load($parentId);
$cat->setPath($parentCategory->getPath());
// $cat->setStoreId(Mage::app()->getStore()->getId());
$cat->setStoreId(0);
$cat->setAttributeSetId($cat->getDefaultAttributeSetId());
try {
$cat->save();
$newCategoryId = $cat->getId();
} catch (Exception $e) {
Mage::logException($e->getMessage());
return;
}
}
if($newCategoryId != '') {
$groupModel = Mage::getModel('core/store_group');
$group = array(
'website_id' => 1,
'name' => 'New Store Gropu',
'root_category_id' => $newCategoryId,
);
$groupModel->setData($group);
$groupModel->setId(null);
$groupModel->save();
Mage::dispatchEvent('store_group_save', array('group' => $groupModel));
$storeModel = Mage::getModel('core/store');
$store = array(
'group_id' => $groupModel->getId(),
'name' => 'New Store',
'code' => 'new_store',
'is_active' => 1,
'sort_order' => 2,
);
$storeModel->setData($store);
$storeModel->setId(null);
$eventName = 'store_add';
$groupModel = Mage::getModel('core/store_group')->load($storeModel->getGroupId());
$storeModel->setWebsiteId($groupModel->getWebsiteId());
$storeModel->save();
Mage::app()->reinitStores(); // or Mage::app()->getConfig()->reinit();
Mage::dispatchEvent($eventName, array('store'=>$storeModel));
}
$installer->endSetup();
$installer->installEntities();
$installer = $this;
$installer->startSetup();
$mobileCategory = Mage::getModel(‚catalog/category‘)->getCollection()
->addAttributeToSelect(‚id‘)
->addAttributeToFilter(’name‘, ‚mobile‘)
->addAttributeToSort(‚id‘, ‚ASC‘)
->distinct(true)
->load();
$mobileCategoryId = “;
if(count($mobileCategory) > 0) {
foreach ($mobileCategory as $value) {
$mobileCategoryId = $value->getId();
}
} else {
$data = array(
’name‘ => ‚Mobile‘,
‚url_key‘ => ‚root‘, //’de‘,
‚description‘ => ‚test category‘,
‚meta_description‘ => ‚test category‘,
‚meta_keywords‘ => ‚categories_meta‘,
‚display_mode‘ => Mage_Catalog_Model_Category::DM_PRODUCT,
‚default_sort_by‘ => Mage::getModel(„catalog/category“)->getDefaultSortBy(),
‚available_sort_by‘ => Mage::getModel(„catalog/category“)->getDefaultSortBy(),
‚is_active‘ => 1,
‚is_anchor‘ => 0,
‚include_in_menu‘ => 0,
‚parent_id‘ => 1,
‚path‘ => 1, //Mage::getModel(„catalog/category“)->load(1)->getPath(),
‚attribute_set_id‘ => Mage::getModel(„catalog/category“)->getDefaultAttributeSetId(),
‚mdm_display_mode‘ => array( ‚0‘ => ’show_products‘),
‚custom_design_apply‘ => 1
);
Mage::app()->getStore()->load(0);
$cat = Mage::getModel(‚catalog/category‘);
$cat->addData($data);
$parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
$parentCategory = Mage::getModel(‚catalog/category‘)->load($parentId);
$cat->setPath($parentCategory->getPath());
// $cat->setStoreId(Mage::app()->getStore()->getId());
$cat->setStoreId(0);
$cat->setAttributeSetId($cat->getDefaultAttributeSetId());
try {
$cat->save();
$mobileCategoryId = $cat->getId();
} catch (Exception $e) {
Mage::logException($e->getMessage());
return;
}
}
if($mobileCategoryId != “) {
$groupModel = Mage::getModel(‚core/store_group‘);
$group = array(
‚website_id‘ => 1,
’name‘ => ‚Mobile‘,
‚root_category_id‘ => $mobileCategoryId, // 119
);
$groupModel->setData($group);
$groupModel->setId(null);
$groupModel->save();
Mage::dispatchEvent(’store_group_save‘, array(‚group‘ => $groupModel));
$storeModel = Mage::getModel(‚core/store‘);
$store = array(
‚group_id‘ => $groupModel->getId(),
’name‘ => ‚Mobile‘,
‚code‘ => ‚mobile‘,
‚is_active‘ => 1,
’sort_order‘ => 2,
);
$storeModel->setData($store);
$storeModel->setId(null);
$eventName = ’store_add‘;
$groupModel = Mage::getModel(‚core/store_group‘)->load($storeModel->getGroupId());
$storeModel->setWebsiteId($groupModel->getWebsiteId());
$storeModel->save();
Mage::app()->reinitStores(); // or Mage::app()->getConfig()->reinit();
Mage::dispatchEvent($eventName, array(’store’=>$storeModel));
}
$installer->endSetup();
$installer->installEntities();