Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an attribute programmatically

Tags:

php

magento

I'd like to add an attribute to products, and I want to do this programmatically. So I added a mysql-install-0.1.0.php to a module of mine, and I added this (inspired from existing examples) :

<?php

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'collection', array(
        'type'              => 'varchar',
        'backend'           => '',
        'frontend'          => '',
        'label'             => 'Collection',
        '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();

Syntax seems OK, but when it comes to execute this part of the code, here is the problem :

Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() in /home/frleq/Dev/projets/AVIP/WORKSPACE/avip_magento/app/code/local/Smile/Magentaho/sql/magentaho_setup/mysql4-install-0.1.0.php on line 7

Do you see what's wrong? Code isn't so complicated, and it is inspired from existing and worling ones...

Thank you

like image 357
frinux Avatar asked Aug 11 '10 14:08

frinux


People also ask

How do I create a product attribute in Magento 2 programmatically using patch?

Create the Data Patch class Lets create the file <Vendor>/<Module_Name>/Setup/Patch/Data/AddVideoToProduct. php in which we define the class AddVideoToProduct . To create the product attribute we need to use Magento\Eav\Setup\EavSetupFactory so we would use DI to inject it into out class.


1 Answers

You are using the wrong setup class. Check your setup class declaration in config.xml. You are using Mage_Core_Model_Resource_Setup. Try Mage_Eav_Model_Entity_Setup instead.

like image 74
Anders Thirsgaard Rasmussen Avatar answered Sep 23 '22 20:09

Anders Thirsgaard Rasmussen