5

I added below code

<?php 


  require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));
$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute  = array(
    'group'                     => 'General',
        'input'                     => 'select',
        'type'                      => 'int',
        'label'                     => 'Mobile (api)',
        'source'                    => 'eav/entity_attribute_source_boolean',
        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                   => 1,
        'required'                  => 0,
        'visible_on_front'          => 0,
        'is_html_allowed_on_front'  => 0,
        'is_configurable'           => 0,
        'searchable'                => 0,
        'filterable'                => 0,
        'comparable'                => 0,
        'unique'                    => false,
        'user_defined'              => false,
        'default'           => '0',
        'is_user_defined'           => false,
        'used_in_product_listing'   => true
);
$installer->addAttribute('catalog_category', 'mobile_api', $attribute);
$installer->endSetup();


?>

in my header.phtml for create custom attribute in category portion. I know it's not a best practice while adding db code in .phtml.

can you please anyone tell me where i have to add?

2 Answers 2

12

Follow bellow steps

Step : 1 app\etc\modules\AR_Dbchange.xml

<?xml version="1.0"?>
<config>
    <modules>
        <AR_Dbchange>
            <active>true</active>
            <codePool>local</codePool>
        </AR_Dbchange>
    </modules>
</config> 

Step : 2 app\code\local\AR\Dbchange\etc\config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <AR_Dbchange>
            <version>0.1.0</version>
        </AR_Dbchange>
    </modules>
    <global>
        <resources>
            <ar_dbchange_setup>
              <setup>
                   <module>AR_Dbchange</module>
                   <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
              </setup>
          </ar_dbchange_setup>
        </resources>  
    </global>
</config> 

Step : 3 app\code\local\AR\Dbchange\sql\ar_dbchange_setup\mysql4-install-0.1.0.php

<?php

$installer = $this;
$installer->startSetup();

$attribute  = array(
    'group'                     => 'General',
        'input'                     => 'select',
        'type'                      => 'int',
        'label'                     => 'Mobile (api)',
        'source'                    => 'eav/entity_attribute_source_boolean',
        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'                   => 1,
        'required'                  => 0,
        'visible_on_front'          => 0,
        'is_html_allowed_on_front'  => 0,
        'is_configurable'           => 0,
        'searchable'                => 0,
        'filterable'                => 0,
        'comparable'                => 0,
        'unique'                    => false,
        'user_defined'              => false,
        'default'           => '0',
        'is_user_defined'           => false,
        'used_in_product_listing'   => true
);
$installer->addAttribute('catalog_category', 'mobile_api', $attribute);
$installer->endSetup();

?>

Step : 4 After refresh all magento cache to admin side (System -> Cache Management) and logout ,login again

2

you can do via custom set up script.

$installer = $this;
$installer->startSetup();

$entityTypeId     = $installer->getEntityTypeId('catalog_category');
$attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'new_cat_attrb',  array(
    'type'     => 'int',
    'label'    => 'New Category Attribute',
    'input'    => 'text',
    'global'   => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'default'           => 0
));


$installer->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'new_cat_attrb',
    '11'                    //last Magento's attribute position in General tab is 10
);

$attributeId = $installer->getAttributeId($entityTypeId, 'new_cat_attrb');

$installer->run("
INSERT INTO `{$installer->getTable('catalog_category_entity_int')}`
(`entity_type_id`, `attribute_id`, `entity_id`, `value`)
    SELECT '{$entityTypeId}', '{$attributeId}', `entity_id`, '1'
        FROM `{$installer->getTable('catalog_category_entity')}`;
");


//this will set data of your custom attribute for root category
Mage::getModel('catalog/category')
    ->load(1)
    ->setImportedCatId(0)
    ->setInitialSetupFlag(true)
    ->save();

//this will set data of your custom attribute for default category
Mage::getModel('catalog/category')
    ->load(2)
    ->setImportedCatId(0)
    ->setInitialSetupFlag(true)
    ->save();

$installer->endSetup();

config.xml

<resources>
    <new_attribute_csv_setup>
      <setup>
        <module>New_Attribute</module>
        <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
      </setup>
      <connection>
        <use>core_setup</use>
      </connection>
    </new_attribute_setup>
    <new_attribute_setup_write>
      <connection>
        <use>core_write</use>
      </connection>
    </new_attribute_setup_write>
    <new_attribute_setup_read>
      <connection>
        <use>core_read</use>
      </connection>
    </new_attribute_setup_read>
</resources>

or you can also visit another solution at bellow link.

https://stackoverflow.com/questions/32733655/magento-programmatically-create-custom-product-attribute-via-set-up-script

I hope this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.