Example #1
0
 protected function _addAttributeToAttributeSet($attributeSetId, $attributeCode)
 {
     $setup = new \Mage_Eav_Model_Entity_Setup('core_setup');
     $attributeId = $setup->getAttributeId('catalog_product', $attributeCode);
     $attributeGroupId = $setup->getDefaultAttributeGroupId('catalog_product', $attributeSetId);
     $setup->addAttributeToSet('catalog_product', $attributeSetId, $attributeGroupId, $attributeId);
 }
<?php

$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute("customer", "complete_sf_form", array("type" => "int", "backend" => "", "label" => "Completed SalesForce Form", "input" => "select", "source" => "eav/entity_attribute_source_boolean", "visible" => 1, "required" => 0, "default" => "0", "user_defined" => 1));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "complete_sf_form");
$setup->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, 'complete_sf_form', '999');
$used_in_forms = array();
$used_in_forms[] = "adminhtml_customer";
$attribute->setData("used_in_forms", $used_in_forms)->setData("is_used_for_customer_segment", true)->setData("is_system", 0)->setData("is_user_defined", 1)->setData("is_visible", 1)->setData("sort_order", 100);
$attribute->save();
$installer->endSetup();
 public function saveAction()
 {
     if ($data = $this->getRequest()->getPost()) {
         $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
         $entityTypeId = $setup->getEntityTypeId('customer');
         $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
         $attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
         $attributeObject = Mage::getModel('customer/attribute');
         $attributeId = $this->getRequest()->getParam('id');
         $helper = Mage::helper('customerattribute');
         $attributeType = $attributeObject->load($attributeId)->getFrontendInput();
         if ($attributeId != null) {
             $data['frontend_input'] = $attributeType;
         }
         $data['validate_rules'] = $helper->getAttributeValidateRules1($data['frontend_input'], $data);
         if ($data['is_custom'] == 1 || $attributeId == 0) {
             if ($data['status'] == 1) {
                 if (in_array(1, $data['display_on_frontend'])) {
                     $data['used_in_forms'][] = 'customer_account_create';
                 }
                 if (in_array(2, $data['display_on_frontend'])) {
                     $data['used_in_forms'][] = 'customer_account_edit';
                 }
                 if ((in_array(3, $data['display_on_frontend']) || in_array(4, $data['display_on_frontend'])) && $attributeType != 'file' && $attributeType != 'image') {
                     $data['used_in_forms'][] = 'checkout_register';
                 }
                 if (in_array(1, $data['display_on_backend'])) {
                     $data['used_in_forms'][] = 'customer_Grid';
                 }
                 if (in_array(2, $data['display_on_backend'])) {
                     $data['used_in_forms'][] = 'order_grid';
                 }
                 if (in_array(3, $data['display_on_backend']) && $attributeType != 'file' && $attributeType != 'image') {
                     $data['used_in_forms'][] = 'adminhtml_checkout';
                 }
                 $data['used_in_forms'][] = 'tabCustomerattribute';
             } else {
                 $data['used_in_forms'][] = array();
             }
         }
         try {
             $data1 = array('status' => $data['status'], 'show_on_create_account' => (int) in_array(1, $data['display_on_frontend']), 'show_on_account_edit' => (int) in_array(2, $data['display_on_frontend']), 'show_on_checkout_register_customer' => (int) in_array(3, $data['display_on_frontend']), 'show_on_checkout_register_guest' => (int) in_array(4, $data['display_on_frontend']), 'show_on_grid_customer' => (int) in_array(1, $data['display_on_backend']), 'show_on_grid_order' => (int) in_array(2, $data['display_on_backend']), 'show_on_admin_checkout' => (int) in_array(3, $data['display_on_backend']), 'customer_group' => implode(", ", $data['customer_group']), 'store_id' => implode(", ", $data['store_view']));
             if ($attributeType == 'file' || $attributeType == 'image') {
                 $data1['show_on_checkout_register_customer'] = 0;
                 $data1['show_on_checkout_register_guest'] = 0;
                 $data1['show_on_admin_checkout'] = 0;
             }
             $model = Mage::getModel('customerattribute/customerattribute');
             if ($attributeId) {
                 $models = Mage::getModel('customerattribute/customerattribute')->getCollection()->addFieldToFilter('attribute_id', $attributeId)->getFirstItem();
                 $data1['attribute_id'] = $attributeId;
                 $attributeObject->load($attributeId)->addData($data);
                 $attributeObject->setId($attributeId)->save();
                 $model->load($models->getId())->setData($data1);
                 $model->setId($models->getId())->save();
                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customerattribute')->__('The customer attribute has been saved.'));
             } else {
                 $data['attribute_code'] = str_replace(' ', '', $data['attribute_code']);
                 $data['attribute_code'] = strtolower($data['attribute_code']);
                 $defaultValueField = $helper->getAttributeDefaultValueByInput($data['frontend_input']);
                 if ($defaultValueField) {
                     $scopeKeyPrefix = $this->getRequest()->getParam('website') ? 'scope_' : '';
                     $data[$scopeKeyPrefix . 'default_value'] = $helper->stripTags($this->getRequest()->getParam($scopeKeyPrefix . $defaultValueField));
                 }
                 $data['entity_type_id'] = $entityTypeId;
                 $data['attribute_group_id'] = $attributeGroupId;
                 $data['attribute_set_id'] = $attributeSetId;
                 $data['backend_type'] = $helper->getAttributeBackendTypeByInputType($data['frontend_input']);
                 $data['backend_model'] = $helper->getAttributeBackendModelByInputType($data['frontend_input']);
                 $data['source_model'] = $helper->getAttributeSourceModelByInputType($data['frontend_input']);
                 $data['is_system'] = 0;
                 $data['is_user_defined'] = 1;
                 if ($data['frontend_input'] == 'date') {
                     $data['input_filter'] = 'date';
                 }
                 $attributeObject->setData($data);
                 $attributeId = $attributeObject->save()->getId();
                 $data1['is_custom'] = 1;
                 $data1['attribute_id'] = $attributeId;
                 $model->setData($data1)->save();
                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('customerattribute')->__('The customer customer attribute has been saved.'));
                 // Addcolumn to orderattribute table create by Hoatq
                 $table = new Mage_Core_Model_Resource_Setup();
                 $table->getConnection()->addColumn($table->getTable('orderattribute'), 'customer_' . $data['attribute_code'], 'varchar(255)');
             }
         } catch (Exception $e) {
             if (!$attributeId) {
                 $attributeCode = $data['attribute_code'];
                 $attributeObject = $this->_initAttribute()->loadByCode($this->_getEntityType()->getId(), $attributeCode);
                 if ($attributeObject->getId()) {
                     $this->_getSession()->addError(Mage::helper('customerattribute')->__('Attribute with the same code already exists'));
                 }
             } else {
                 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('customerattribute')->__('Unable to find item to save'));
             }
         }
         if ($this->getRequest()->getParam('back', false)) {
             $this->_redirect('*/*/edit', array('id' => $attributeId, '_current' => true));
             return;
         } else {
             $this->_redirect('*/*/');
             return;
         }
     } else {
         Mage::getSingleton('adminhtml/session')->addError(Mage::helper('customerattribute')->__('Unable to find item to save'));
     }
     $this->_redirect('*/*/');
     return;
 }
<?php

/**
 * @author Amasty Team
 * @copyright Copyright (c) 2015 Amasty (https://www.amasty.com)
 * @package Amasty_Xnotif
 */
$installer = new Mage_Eav_Model_Entity_Setup($this->_resourceName);
$installer->startSetup();
$installer->addAttribute('catalog_product', 'amxnotif_hide_alert', array('type' => 'int', 'backend' => '', 'frontend' => '', 'label' => 'Hide Stock Alert Block', 'input' => 'select', 'class' => '', 'source' => 'eav/entity_attribute_source_boolean', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 'visible' => true, 'required' => false, 'user_defined' => false, 'default' => '', 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'unique' => false, 'is_configurable' => false));
$attributeId = $installer->getAttributeId('catalog_product', 'amxnotif_hide_alert');
foreach ($installer->getAllAttributeSetIds('catalog_product') as $attributeSetId) {
    try {
        $attributeGroupId = $installer->getAttributeGroupId('catalog_product', $attributeSetId, 'General');
    } catch (Exception $e) {
        $attributeGroupId = $installer->getDefaultAttributeGroupId('catalog_product', $attributeSetId);
    }
    $installer->addAttributeToSet('catalog_product', $attributeSetId, $attributeGroupId, $attributeId);
}
$installer->endSetup();
// 'air_flow'=>'Air Flow (m3/h)',
// 'noise_level'=>'Noise Level',
// 'speed'=>'Speed',
// 'control'=>'Control',
// 'filter'=>'Filter',
// 'finish'=>'Finish',
// 'timer'=>'Timer',
// 'no_of_lamps'=>'No.Of Lamps',
// 'burner'=>'Burner (no)',
// 'triple_ring_burner'=>'Triple Ring Burner',
// 'flat_burner'=>'Flat Burner',
// 'warranty'=>'Warranty',
// 'type'=>'Type',
// 'no_of_ways'=>'No.Of Ways',
// 'no_of_pin'=>'No.Of Pin',
// 'no_of_modules'=>'No.Of Modules',
// 'ampere'=>'Ampere',
// 'trap_type'=>'Trap Type',
Mage::app('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $installer->getEntityTypeId('catalog_product');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
foreach ($attributes as $key => $value) {
    try {
        $installer->addAttribute('catalog_product', $key, array('group' => 'Specification', 'type' => 'varchar', 'backend' => 'eav/entity_attribute_backend_array', 'frontend' => '', 'class' => '', 'default' => '', 'label' => $value, 'input' => 'text', 'source' => '', 'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE, 'is_visible' => 1, 'required' => 0, 'searchable' => 1, 'filterable' => 0, 'unique' => 0, 'comparable' => 0, 'visible_on_front' => 1, 'user_defined' => 1));
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}