B-219 Sec-55 Noida, India
+918010221733

Fetch configurable attribute from CRE Loaded database to Magento database

Here is the code to fetch the attribute (that should be configurable in magento database) from the CRE Loaded database to magento database
///////////////////////////////CODE/////////////////////////////////
require_once ‘/app/Mage.php’;
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
/////////////////////Create Configurable Attribute From CRE Loaded Database///////////////////////
function createAttribute($code, $label, $attribute_type, $product_type)
{
$_attribute_data = array(
‘attribute_code’ => $code,
‘is_global’ => ’1′,
‘frontend_input’ => $attribute_type, //’boolean’,
‘default_value_text’ => ”,
‘default_value_yesno’ => ’0′,
‘default_value_date’ => ”,
‘default_value_textarea’ => ”,
‘is_unique’ => ’0′,
‘is_required’ => ’0′,
‘apply_to’ => array($product_type), //array(‘grouped’)
‘is_configurable’ => ’1′,
‘is_searchable’ => ’0′,
‘is_visible_in_advanced_search’ => ’0′,
‘is_comparable’ => ’0′,
‘is_used_for_price_rules’ => ’0′,
‘is_wysiwyg_enabled’ => ’0′,
‘is_html_allowed_on_front’ => ’1′,
‘is_visible_on_front’ => ’0′,
‘used_in_product_listing’ => ’0′,
‘used_for_sort_by’ => ’0′,
‘frontend_label’ => $label
);
$model = Mage::getModel(‘catalog/resource_eav_attribute’);
if (!isset($_attribute_data[‘is_configurable’])) {
$_attribute_data[‘is_configurable’] = 0;
}
if (!isset($_attribute_data[‘is_filterable’])) {
$_attribute_data[‘is_filterable’] = 0;
}
if (!isset($_attribute_data[‘is_filterable_in_search’])) {
$_attribute_data[‘is_filterable_in_search’] = 0;
}
if (is_null($model->getIsUserDefined()) || $model->getIsUserDefined() != 0) {
$_attribute_data[‘backend_type’] = $model->getBackendTypeByInput($_attribute_data[‘frontend_input’]);
}
$defaultValueField = $model->getDefaultValueByInput($_attribute_data[‘frontend_input’]);
if ($defaultValueField) {
$_attribute_data[‘default_value’] = $this->getRequest()->getParam($defaultValueField);
}
$model->addData($_attribute_data);
$model->setEntityTypeId(Mage::getModel(‘eav/entity’)->setType(‘catalog_product’)->getTypeId());
$model->setIsUserDefined(1);
try {
$model->save();
} catch (Exception $e) { echo ‘
Sorry, error occured while trying to save the attribute. Error: ‘.$e->getMessage().’
‘; }
}
////////////////your cre loaded database connection/////////////
$conn = mysql_connect(‘localhost’,’root’,”) ;
mysql_select_db(‘shnoop_cre’,$conn);
//////////////////////////////////////////////////////////////
$select_attributes = @mysql_query(“select * from products_options order by products_options_id”);
while($fetch_attribute = @mysql_fetch_object($select_attributes))
{
if($fetch_attribute->options_type==0)
$input_type=’select’;
elseif($fetch_attribute->options_type==1)
$input_type=’text’;
elseif($fetch_attribute->options_type==2)
$input_type=’radio’;
elseif($fetch_attribute->options_type==3)
$input_type=’checkbox’;
elseif($fetch_attribute->options_type==4)
$input_type=’textarea’;
$Attr_label = $fetch_attribute->products_options_name;
$fetch_attribute->products_options_name = str_replace(‘ ‘,”,$fetch_attribute->products_options_name);
$fetch_attribute->products_options_name = str_replace(‘/’,’_’,$fetch_attribute->products_options_name);
$fetch_attribute->products_options_name = strtolower($fetch_attribute->products_options_name);
createAttribute($fetch_attribute->products_options_name,$Attr_label,$input_type, “configurable”);
}
//////////////////////////////////////////////////////////////////////
//////////////////////Fetch Configurable Attribute Options From CRE Loaded Database To Magento Database/////////////////////////////////
////////////////your cre loaded database connection/////////////
$conn = mysql_connect(‘localhost’,’root’,”) ;
mysql_select_db(‘shnoop_cre’,$conn);
//////////////////////////////////////////////////////////////
$select_attributes = @mysql_query(“select * from products_options order by products_options_id”);
while($fetch_attribute = @mysql_fetch_object($select_attributes))
{
$fetch_attribute->products_options_name = str_replace(‘ ‘,”,$fetch_attribute->products_options_name);
$fetch_attribute->products_options_name = str_replace(‘/’,’_’,$fetch_attribute->products_options_name);
$fetch_attribute->products_options_name = strtolower($fetch_attribute->products_options_name);
$_newOptions = Array();
$select_attribut_options = @mysql_query(“select * from products_options_values_to_products_options where products_options_id=’$fetch_attribute->products_options_id’”);
while($fetch_attribut_option=@mysql_fetch_object($select_attribut_options))
{
$fetch_attribut_option_value = @mysql_fetch_object(mysql_query(“select * from products_options_values where products_options_values_id=’$fetch_attribut_option->products_options_values_id’”));
if(!in_array($fetch_attribut_option_value->products_options_values_name,$_newOptions))
array_push($_newOptions,$fetch_attribut_option_value->products_options_values_name);
}
//Array of values that will become the list of colours
$_attribute = Mage::getModel(‘eav/entity_attribute’)->loadByCode(‘catalog_product’, $fetch_attribute->products_options_name); //change ‘primarycolour’ to any attribute
$_oldOptionArr = array(‘value’=>array(), ‘order’=>array(), ‘delete’=>array());
foreach ( $_attribute->getSource()->getAllOptions(true, true) as $option){
$_oldOptionArr[‘value’][$option[‘value’]] = array($option[‘label’]);
}
$_newOptionArr = array(‘value’=>array(), ‘order’=>array(), ‘delete’=>array());
$i = 0;
foreach ($_newOptions as $_newOption)
{
$i++;
echo $_newOption;
if(!in_array(Array($_newOption), $_oldOptionArr[‘value’]) && $_newOption != ”) //If the option doesn’t exist already in Magento…
{
$_newOptionArr[‘value’][‘option_’ . $i] = array($_newOption); //Add the option to the new array
}
}
$_attribute->setOption($_newOptionArr);
$_attribute->save();
}
//////////////////////////////////////////////////////////////

(Visited 64 times, 1 visits today)

Leave a reply

You must be logged in to post a comment.