2
votes

I am trying to run a script that creates additional product attribute in magento admin. However the attribute doesn't come out to the admin backend.

This is my model: Setup.php

class Rts_Cattribute_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
protected function _prepareValues($attr)
{
    $data = parent::_prepareValues($attr);
    $data = array_merge($data, array(

        'apply_to'                      => $this->_getValue($attr, 'apply_to'),
        'frontend_input_renderer'       => $this->_getValue($attr, 'input_renderer'),
        'is_comparable'                 => $this->_getValue($attr, 'comparable', 0),
        'is_configurable'               => $this->_getValue($attr, 'is_configurable', 1),
        'is_filterable'                 => $this->_getValue($attr, 'filterable', 0),
        'is_filterable_in_search'       => $this->_getValue($attr, 'filterable_in_search', 0),
        'is_global'                     => $this->_getValue(
            $attr,
            'global',
            Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE
        ),
        'is_html_allowed_on_front'      => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
        'is_searchable'                 => $this->_getValue($attr, 'searchable', 0),
        'is_used_for_promo_rules'       => $this->_getValue($attr, 'used_for_promo_rules', 0),
        'is_visible'                    => $this->_getValue($attr, 'visible', 1),
        'is_visible_on_front'           => $this->_getValue($attr, 'visible_on_front', 1),
        'is_wysiwyg_enabled'            => $this->_getValue($attr, 'wysiwyg_enabled', 0),
        'is_visible_in_advanced_search' => $this->_getValue($attr, 'visible_in_advanced_search', 0),
        'position'                      => $this->_getValue($attr, 'position', 0),
        'used_for_sort_by'              => $this->_getValue($attr, 'used_for_sort_by', 0),
        'used_in_product_listing'       => $this->_getValue($attr, 'used_in_product_listing', 0)
    ));
    return $data;
}
}

This is my config: config.xml

<config>
<modules>
    <Rts_Cattribute>
        <version>0.1.0</version>
    </Rts_Cattribute>
</modules>
<global>
    <resources>
        <cattribute_setup>
            <setup>
                <module>Rts_Cattribute</module>
                <class>Rts_Cattribute_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
        </cattribute_setup>
    </resources>
</global>
</config>

Lastly this is my install script: mysql4-install-0.1.0.php

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'max_ftlbs', array(
    'type'              => 'int',
    'backend'           => '',
    'frontend'          => '',
    'label'             => 'Max Ft.Lbs',
    'input'             => 'text',
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => false,
    'required'          => false,
    'user_defined'      => false,
    'default'           => '',
    'searchable'        => false,
    'filterable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false
));

$installer->endSetup();

I followed this tutorials

http://magento4newbies.blogspot.com/2015/01/how-to-adding-custom-product-attributes.html

http://codegento.com/2011/02/install-scripts-and-upgrade-scripts/

In this link Adding custom product attributes in Magento using setup script it says that The standard class for magento installers is Mage_Eav_Model_Entity_Setup but when dealing with products, you'll need to use Mage_Catalog_Model_Resource_Setup instead.

I've tried many solution however it didn't work.

Everytime I do some changes I always delete the resource version before I refresh the page.

Problem:

The custom attribute won't show up or it is not created in the admin backend.

Question:

What is the right standard class for magento installer?

Is my installer correct?

I hope you can help me with this.

4
Do you have the xml file in app/etc/modules? e.g. Rts_Cattribute.xml <modules> <Rts_Cattribute> <version>0.1.0</version> </Rts_Cattribute> </modules>Marko Novakovic
Yes, I already included it, however the attribute don't display but the version was already in the core_resource.rodge
Maybe you need to reindex and clear the cache. Have you checked the attribute in the DB?Marko Novakovic
I always clear my cache but the attribute was not created however the version was registered on the core_resource table.rodge
If you ran the setup once, and a row is in the setup table for your version, the setup script won't run a second time. You need to remove that from the database.Biagio Arobba

4 Answers

1
votes

One thing i noticed I had to do was put the db connection info in the config.xml class.

<catalog_setup>
            <setup>
                <module>XX_YY</module>
                <class>XX_YY_Model_Resource_Eav_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </catalog_setup>
        <catalog_setup>
            <connection>
                <use>core_write</use>
            </connection>
        </catalog_setup>
        <catalog_setup>
            <connection>
                <use>core_read</use>
            </connection>
        </catalog_setup>

Also in the installer class I set installer equal to this:

$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
1
votes

For Create Product Attribute no need to create Module just run below script in your Magento root folder.

<?php  

require_once('app/Mage.php');
 Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Eav_Model_Entity_Setup('core_setup');
$installer->startSetup();                   

$installer->addAttribute('catalog_product', 'max_ftlbs', array(
     'type'              => 'int',
     'backend'           => '',
     'frontend'          => '',
     'label'             => 'Max Ft.Lbs',
     'input'             => 'text',
     'class'             => '',
     'source'            => '',
     'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
     'visible'           => false,
     'required'          => false,
     'user_defined'      => false,
     'default'           => '',
     'searchable'        => false,
     'filterable'        => false,
     'comparable'        => false,
     'visible_on_front'  => false,
     'unique'            => false,
     'apply_to'          => '',
     'is_configurable'   => false
));

$installer->endSetup();

?>

For Remove Product Attribute

<?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;
 $installer->startSetup();
 $installer->removeAttribute('catalog_product', 'max_ftlbs');
 $installer->endSetup();

?>
1
votes

If you use the script to create the custom attribute you need to run the script each time when you are moving the files from one database to another (Local-Staging-Live ).

But we create using the module it is very easy to maintain and manage.

Thanks

0
votes

This tutorial gives step by step explanation of how to add custom product attribute and the file structure using setup script.

http://www.pearlbells.co.uk/adding-custom-product-attributes-in-magento-using-setup-script/

<?php
$this->startSetup();
$this->addAttribute(catalog_product, 'featured_product', array(
'group'         => 'General',
'input'         => 'select',
'type'          => 'text',
'label'         => 'Featured Product',
'backend'       => '',
'visible'       => true,
'required'      => false,
'visible_on_front' => true,
'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'source' => 'eav/entity_attribute_source_boolean',
'sort_order'        => 8,
));

$this->endSetup();