URL: http://alanstorm.com/magento_attribute_migration_generator
#/usr/bin/env php
<?php
function bootstrap()
{
/**
* Error reporting
*/
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
$maintenanceFile = 'maintenance.flag';
require_once $mageFilename;
#Varien_Profiler::enable();
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
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::init($mageRunCode, $mageRunType);
return $mageFilename;
}
function get_option_array_for_attribute($attribute)
{
$read = Mage::getModel('core/resource')->getConnection('core_read');
$select = $read->select()
->from('eav_attribute_option')
->join('eav_attribute_option_value','eav_attribute_option.option_id=eav_attribute_option_value.option_id')
->where('attribute_id=?',$attribute->getId())
->where('store_id=0')
->order('eav_attribute_option_value.option_id');
$query = $select->query();
$values = array();
foreach($query->fetchAll() as $rows)
{
$values[] = $rows['value'];
}
//$values = array('#f00000','abc123');
return array('values'=>$values);
}
function get_key_legend()
{
return array(
//catalog
'frontend_input_renderer' => 'input_renderer',
'is_global' => 'global',
'is_visible' => 'visible',
'is_searchable' => 'searchable',
'is_filterable' => 'filterable',
'is_comparable' => 'comparable',
'is_visible_on_front' => 'visible_on_front',
'is_wysiwyg_enabled' => 'wysiwyg_enabled',
'is_visible_in_advanced_search' => 'visible_in_advanced_search',
'is_filterable_in_search' => 'filterable_in_search',
'is_used_for_promo_rules' => 'used_for_promo_rules',
'backend_model' => 'backend',
'backend_type' => 'type',
'backend_table' => 'table',
'frontend_model' => 'frontend',
'frontend_input' => 'input',
'frontend_label' => 'label',
'source_model' => 'source',
'is_required' => 'required',
'is_user_defined' => 'user_defined',
'default_value' => 'default',
'is_unique' => 'unique',
'is_global' => 'global',
);
}
function get_migration_script_for_attribute($code)
{
//load the existing attribute model
$m = Mage::getModel('catalog/resource_eav_attribute')
->loadByCode('catalog_product',$code);
//get a map of "real attribute properties to properties used in setup resource array
$real_to_setup_key_legend = get_key_legend();
//swap keys from above
$data = $m->getData();
$keys_legend = array_keys($real_to_setup_key_legend);
$new_data = array();
foreach($data as $key=>$value)
{
if(in_array($key,$keys_legend))
{
$key = $real_to_setup_key_legend[$key];
}
$new_data[$key] = $value;
}
//unset items from model that we don't need and would be discarded by
//resource script anyways
$attribute_code = $new_data['attribute_code'];
unset($new_data['attribute_id']);
unset($new_data['attribute_code']);
unset($new_data['entity_type_id']);
//chuck a few warnings out there for things that were a little murky
if($new_data['attribute_model'])
{
echo "//WARNING, value detected in attribute_model. We've never seen a value there before and this script doesn't handle it. Caution, etc. " . "\n";
}
if($new_data['is_used_for_price_rules'])
{
echo "//WARNING, non false value detected in is_used_for_price_rules. The setup resource migration scripts may not support this (per 1.7.0.1)" . "\n";
}
//load values for attributes (if any exist)
$new_data['option'] = get_option_array_for_attribute($m);
//get text for script
$array = var_export($new_data, true);
$script = "<?php
if(! (\$this instanceof Mage_Catalog_Model_Resource_Setup) )
{
throw new Exception(\"Resource Class needs to inherit from \" .
\"Mage_Catalog_Model_Resource_Setup for this to work\");
}
\$attr = $array;
\$this->addAttribute('catalog_product','$attribute_code',\$attr);
";
return $script;
}
function usage()
{
echo "USAGE: magento-create-setup.php attribute_code" . "\n";
}
function main($argv)
{
$script = array_shift($argv);
$code = array_shift($argv);
if(!$code)
{
usage();
exit;
}
$script = get_migration_script_for_attribute($code);
echo $script;
}
bootstrap();
main($argv);
/*PHP CLI*/
php magento-create-setup.php gender
<?php
if(! ($this instanceof Mage_Catalog_Model_Resource_Setup) )
{
throw new Exception("Resource Class needs to inherit from " .
"Mage_Catalog_Model_Resource_Setup for this to work");
}
$attr = array (
'attribute_model' => NULL,
'backend' => NULL,
'type' => 'int',
'table' => NULL,
'frontend' => NULL,
'input' => 'select',
'label' => 'Gender',
'frontend_class' => '',
'source' => 'eav/entity_attribute_source_table',
'required' => '1',
'user_defined' => '1',
'default' => NULL,
'unique' => '0',
'note' => '',
'input_renderer' => NULL,
'global' => '1',
'visible' => '1',
'searchable' => '1',
'filterable' => '0',
'comparable' => '0',
'visible_on_front' => '0',
'is_html_allowed_on_front' => '0',
'is_used_for_price_rules' => '1',
'filterable_in_search' => '0',
'used_in_product_listing' => '0',
'used_for_sort_by' => '0',
'is_configurable' => '1',
'apply_to' => 'simple,grouped,configurable',
'visible_in_advanced_search' => '1',
'position' => '1',
'wysiwyg_enabled' => '0',
'used_for_promo_rules' => '1',
'option' =>
array (
'values' =>
array (
0 => 'Womens',
1 => 'Mens',
2 => 'Boys',
3 => 'Girls',
),
),
);
$this->addAttribute('catalog_product','gender',$attr);