Alex
Alex

Reputation: 1250

Magento add new attribute to product

I'm trying to add a new attribute to all the products, after reading a lot I got to this code:

<?php

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

$attribute = array(
    'group'             => 'General',
    'type'              => 'boolean',
    'source'            => 'eav/entity_attribute_source_boolean',
    'sort_order'        => 7,
    'label'             => 'producto destacado',
    'input'             => 'select',
    'class'             => 'validate-number',
    'backend'           => '',
    'frontend'          => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => false,
    'searchable'        => true,
    'filterable'        => true,
    'comparable'        => true,
    'visible_on_front'  => true,
    'visible_in_advanced_search' => true,
    'unique'            => false,
);

$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY, 'is_featured', $attribute);
$installer->endSetup();

But this drives me to an error:

Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() in /Applications/MAMP/htdocs/app/code/local/Feliu/Featuredproducts/sql/feliu_featuredproducts_setup/install-0.1.0.php on line 34

So $installer->addAttribute() is not working and I'm not sure why. I made it yesterday adding an attribute to all categories and it worked.

I was trying with $installer->addAttribute('catalog_product', 'is_featured', $attribute); before, but I read about using Mage_Catalog_Model_Product::ENTITY instead of catalog_product and I changed it. I see the same error message with both of them.

I think I'm missing something important, but just now I can't see what it is. :-S

PD: I'm using magento 1.9.3

Upvotes: 1

Views: 707

Answers (1)

Mladen Ilić
Mladen Ilić

Reputation: 1775

You are using wrong setup model. Mage_Core_Model_Resource_Setup indeed does not have addAttribute method.

You should use Mage_Catalog_Model_Resource_Setup to add attributes to products. You can change setup model used from config.xml file:

<?xml version="1.0"?>
<config>
    <global>
        ...
        <resources>
            <namespace_module_setup>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
            </namespace_module_setup>
        </resources>
        ...
    </global>
</config>

Hope this helps. :)

Upvotes: 1

Related Questions