Skip to content

Instantly share code, notes, and snippets.

@magento-team
Created November 8, 2012 17:39
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save magento-team/4040304 to your computer and use it in GitHub Desktop.
Save magento-team/4040304 to your computer and use it in GitHub Desktop.
Create 2500 configurable products, Magento CE 1.7.0.1
<?php
/**
* Error reporting
*/
error_reporting(E_ALL | E_STRICT);
/**
* Compilation includes configuration file
*/
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
Mage::setIsDeveloperMode(true);
}
umask(0);
/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::app();
/* Create attribute */
/** @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = new Mage_Catalog_Model_Resource_Setup('catalog_setup');
/** @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
$attribute = Mage::getResourceModel('catalog/eav_attribute');
$attribute->load('test_configurable', 'attribute_code');
if ($attribute->getId() == null) {
$attribute->setData(
array(
'attribute_code' => 'test_configurable',
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
'is_global' => 1,
'is_user_defined' => 1,
'frontend_input' => 'select',
'is_unique' => 0,
'is_required' => 1,
'is_configurable' => 1,
'is_searchable' => 0,
'is_visible_in_advanced_search' => 0,
'is_comparable' => 0,
'is_filterable' => 0,
'is_filterable_in_search' => 0,
'is_used_for_promo_rules' => 0,
'is_html_allowed_on_front' => 1,
'is_visible_on_front' => 0,
'used_in_product_listing' => 0,
'used_for_sort_by' => 0,
'frontend_label' => array(
0 => 'Test Configurable'
),
'option' => array(
'value' => array(
'option_0' => array(0 => 'Option 1'),
'option_1' => array(0 => 'Option 2'),
),
'order' => array(
'option_0' => 1,
'option_1' => 2,
)
),
'backend_type' => 'int',
)
);
$attribute->save();
/* Assign attribute to attribute set */
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
}
/* Create simple products per each option */
/** @var $options Mage_Eav_Model_Resource_Entity_Attribute_Option_Collection */
$options = Mage::getResourceModel('eav/entity_attribute_option_collection');
$options->setAttributeFilter($attribute->getId());
for ($i = 1; $i <= 2500; $i++) {
$attributeValues = array();
$productsData = array();
foreach ($options as $option) {
/** @var $product Mage_Catalog_Model_Product */
$product = Mage::getModel('catalog/product');
$product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE)
->setId($i * 10000 + $option->getId() * 10)
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
->setWebsiteIds(array(1))
->setName('Configurable Option' . $option->getId() . '-' . $i)
->setSku('simple_'.$option->getId() . '_' . $i)
->setPrice(10)
->setTestConfigurable($option->getId())
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE)
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setStockData(
array(
'use_config_manage_stock' => 1,
'qty' => 100,
'is_qty_decimal' => 0,
'is_in_stock' => 1,
)
)
->save();
$dataOption = array(
'label' => 'test',
'attribute_id' => $attribute->getId(),
'value_index' => $option->getId(),
'is_percent' => false,
'pricing_value' => 5,
);
$productsData[$product->getId()] = array($dataOption);
$attributeValues[] = $dataOption;
}
/** @var $product Mage_Catalog_Model_Product */
$product = Mage::getModel('catalog/product');
$product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE)
->setId($i)
->setAttributeSetId($installer->getAttributeSetId('catalog_product', 'Default'))
->setWebsiteIds(array(1))
->setName('Configurable Product')
->setSku('configurable_' . $i)
->setPrice(100)
->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->setStockData(
array(
'use_config_manage_stock' => 1,
'is_in_stock' => 1,
)
)
->setConfigurableProductsData($productsData)
->setConfigurableAttributesData(
array(
array(
'attribute_id' => $attribute->getId(),
'attribute_code'=> $attribute->getAttributeCode(),
'frontend_label'=> 'test',
'values' => $attributeValues,
)
)
)
->save();
echo '.';
if ($i % 100 == 0) {
echo "\n$i\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment