Created
September 1, 2014 13:31
-
-
Save jzahedieh/9101ab18f53ab21934da to your computer and use it in GitHub Desktop.
Website generator with products
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
set_time_limit(0); | |
require_once 'abstract.php'; | |
class Website_Generator extends Mage_Shell_Abstract | |
{ | |
protected $_start = 1; | |
protected $_create = 5000; | |
public function run() | |
{ | |
for ($this->_start; $this->_start <= $this->_create; $this->_start++) { | |
$this->_createWebsite("website$this->_start"); | |
echo "\n"; | |
} | |
} | |
protected function _createWebsite($name) | |
{ | |
// Create the root catalog | |
$category = Mage::getModel('catalog/category'); | |
$category->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID); | |
$category->setData('name', ucfirst($name) . ' Catalog'); | |
$category->setData('url_key', $name . '-catalog'); | |
$category->setData('display_mode', 'PRODUCTS'); | |
$category->setData('level', 1); | |
$category->setData('is_active', 1); | |
$category->setData('include_in_menu', 1); | |
$category->setData('is_anchor', 0); | |
$category->setData('custom_use_parent_settings', 0); | |
$category->setData('custom_apply_to_products', 0); | |
$category->setData('attribute_set_id', $category->getDefaultAttributeSetId()); | |
$category->save(); | |
$category->setParentId(1); | |
$category->setPath('1/' . $category->getId()); | |
$category->save(); | |
// Create the sub catalog | |
$subcategory = Mage::getModel('catalog/category'); | |
$subcategory->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID); | |
$subcategory->setData('name', 'Example Category'); | |
$subcategory->setData('url_key', $name . '-catalog'); | |
$subcategory->setData('display_mode', 'PRODUCTS'); | |
$subcategory->setData('level', 2); | |
$subcategory->setData('is_active', 1); | |
$subcategory->setData('include_in_menu', 1); | |
$subcategory->setData('is_anchor', 0); | |
$subcategory->setData('custom_use_parent_settings', 0); | |
$subcategory->setData('custom_apply_to_products', 0); | |
$subcategory->setData('attribute_set_id', $subcategory->getDefaultAttributeSetId()); | |
$subcategory->save(); | |
$subcategory->setParentId($category->getId()); | |
$subcategory->setPath($category->getPath() . '/' . $subcategory->getId()); | |
$subcategory->save(); | |
// Load the website | |
$website = Mage::getModel('core/website')->load($name, 'code'); | |
if ($website->getId() > 0) { | |
$this->_echoMessage("Website already exists"); | |
$this->_echoMessage("Updating store-group"); | |
$group = Mage::getModel('core/store_group')->load($website->getId(), 'website_id'); | |
$group->setData('root_category_id', $category->getId()); | |
$group->save(); | |
return; | |
} | |
// Create the website | |
$this->_echoMessage("Creating new website"); | |
$website->setData('name', ucfirst($name) . ' Website'); | |
$website->setData('code', $name); | |
$website->setData('sort_order', 1); | |
$website->setData('is_default', 0); | |
$website->save(); | |
$websiteId = $website->getWebsiteId(); | |
// Create the group | |
$this->_echoMessage("Creating new store-group"); | |
$group = Mage::getModel('core/store_group')->load($websiteId, 'website_id'); | |
$group->setData('website_id', $websiteId); | |
$group->setData('name', ucfirst($name) . ' Store'); | |
$group->setData('root_category_id', $category->getId()); | |
$group->save(); | |
$groupId = $group->getGroupId(); | |
// Create the store | |
$this->_echoMessage("Creating new store"); | |
$store = Mage::getModel('core/store'); | |
$store->setData('website_id', $websiteId); | |
$store->setData('group_id', $groupId); | |
$store->setData('name', ucfirst($name) . ' Store'); | |
$store->setData('code', $name); | |
$store->setData('is_active', 1); | |
$store->save(); | |
$storeId = $store->getStoreId(); | |
// Update the website | |
$this->_echoMessage("Updating website"); | |
$website->setData('default_group_id', $groupId); | |
$website->save(); | |
// Update the group | |
$this->_echoMessage("Updating store-group"); | |
$group->setData('default_store_id', $storeId); | |
$group->save(); | |
// Add Products to Website | |
$this->_echoMessage("Updating website/category products"); | |
$products = Mage::getModel('catalog/product')->getCollection(); | |
/* @var $product Mage_Catalog_Model_Product*/ | |
foreach ($products as $product) { | |
$current_ids = $product->getWebsiteIds(); | |
$cat_ids = $product->getCategoryIds(); | |
$current_ids[] = $websiteId; | |
$cat_ids[] = $subcategory->getId(); | |
$product->setWebsiteIds($current_ids); | |
$product->setCategoryIds($cat_ids); | |
$product->save(); | |
} | |
} | |
protected function _echoMessage($message) | |
{ | |
echo sprintf("%d: %s \r", $this->_start, $message); | |
} | |
} | |
$shell = new Website_Generator(); | |
$shell->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment