3

I've created a new attribute for a module

app/code/local/AtlanticBT/Featured/Model/Resource/Eav/Mysql4/Setup.php

<?php
class AtlanticBT_Featured_Model_Resource_Eav_Mysql4_Setup
  extends Mage_Eav_Model_Entity_Setup
{
    public function getDefaultEntities()
    {
        return array(
            'catalog_product' => array(
                'entity_model'      => 'catalog/product',
                'attribute_model'   => 'catalog/resource_eav_attribute',
                'table'             => 'catalog/product',
                'additional_attribute_table' => 'catalog/eav_attribute',
                'entity_attribute_collection' => 'catalog/product_attribute_collection',
                'attributes' => array(
                    'featured' => array(
                        'group'                      => 'General',
                        'type'                       => 'int',
                        'backend'                    => '',
                        'frontend'                   => '',
                        'label'                      => 'Featured',
                        'input'                      => 'select',
                        'class'                      => '',
                        'source'                     => 'eav/entity_attribute_source_boolean',
                        'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
                        'visible'                    => true,
                        'required'                   => false,
                        'user_defined'               => true,
                        'default'                    => false,
                        'searchable'                 => true,
                        'filterable'                 => true,
                        'comparable'                 => false,
                        'visible_on_front'           => true,
                        'visible_in_advanced_search' => true,
                        'used_in_product_listing'    => true,
                        'used_for_sort_by'           => true,
                        'unique'                     => false,
                    ),
                ),
            ),
        );
    }
}

app/code/local/AtlanticBT/Featured/sql/featured_setup/mysql4-install-0.1.0.php

<?php
$this->installEntities();

can I alter this attribute to only show on configurable products? would that be a class in the backend or frontend option which controls this functionality?

  • 1
    BTW - you should never use the Setup.php file to install attributes. Doing it in this manner is very unflexible for upgrading in the future. I highly recommend using your sql upgrade scripts to add/modify attributes. – Joseph at SwiftOtter Feb 26 '13 at 2:47
  • why is it unflexible for upgrades in the future? – veilig Mar 1 '13 at 2:36
  • Good question :). Doing it this way removes the versioning that the sql scripts allow for. You add an attribute, and then what happens when you need to remove the attribute? You have all of the functionality of the setup class in the sql scripts, there is just the extra layer of abstraction that allows this extra customization. Does that make sense? – Joseph at SwiftOtter Mar 1 '13 at 14:15
  • I also did it this way. Anyone has a link on how to add a custom attribute to a product in the proper way? – Guus Aug 15 '13 at 11:52
3

@paul pointed me in the right direction, but was actually incomplete in version 1.7.0.2

I had to extend an addition method _prepareValues in my Setup.php file

<?php
class AtlanticBT_Featured_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'),
            )
        );

        return $data;
    }

    public function getDefaultEntities()
    {
        return array(
                'catalog_product' => array(
                    'entity_model'      => 'catalog/product',
                    'attribute_model'   => 'catalog/resource_eav_attribute',
                    'table'             => 'catalog/product',
                    'additional_attribute_table' => 'catalog/eav_attribute',
                    'entity_attribute_collection' => 'catalog/product_attribute_collection',
                    'attributes' => array(
                        'featured' => array(
                            'group'                      => 'General',
                            'type'                       => 'int',
                            'backend'                    => '',
                            'frontend'                   => '',
                            'label'                      => 'Featured',
                            'input'                      => 'select',
                            'class'                      => '',
                            'source'                     => 'eav/entity_attribute_source_boolean',
                            'global'                     => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
                            'visible'                    => true,
                            'required'                   => false,
                            'user_defined'               => true,
                            'default'                    => false,
                            'searchable'                 => true,
                            'filterable'                 => true,
                            'comparable'                 => false,
                            'visible_on_front'           => true,
                            'visible_in_advanced_search' => true,
                            'used_in_product_listing'    => true,
                            'used_for_sort_by'           => true,
                            'unique'                     => false,
                            'apply_to'                   => 'configurable,simple',
                        ),
                    ),
                ),
            );
    }
}
| improve this answer | |
  • Can you please accept either your answer or the one that pushed you towards the solution? – user487772 Jun 16 '13 at 12:10
2

I see you've found a solution to your problem, but I thought it worth to add a note:

Since Magento 1.7 you should use Mage_Catalog_Model_Resource_Setup instead of Mage_Eav_Model_Entity_Setup and it will deal with the apply_to setting and many others:

protected function _prepareValues($attr)
{
    $data = parent::_prepareValues($attr);
    $data = array_merge($data, array(
        'frontend_input_renderer'       => $this->_getValue($attr, 'input_renderer'),
        'is_global'                     => $this->_getValue(
            $attr,
            'global',
            Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL
        ),
        'is_visible'                    => $this->_getValue($attr, 'visible', 1),
        'is_searchable'                 => $this->_getValue($attr, 'searchable', 0),
        'is_filterable'                 => $this->_getValue($attr, 'filterable', 0),
        'is_comparable'                 => $this->_getValue($attr, 'comparable', 0),
        'is_visible_on_front'           => $this->_getValue($attr, 'visible_on_front', 0),
        'is_wysiwyg_enabled'            => $this->_getValue($attr, 'wysiwyg_enabled', 0),
        'is_html_allowed_on_front'      => $this->_getValue($attr, 'is_html_allowed_on_front', 0),
        'is_visible_in_advanced_search' => $this->_getValue($attr, 'visible_in_advanced_search', 0),
        'is_filterable_in_search'       => $this->_getValue($attr, 'filterable_in_search', 0),
        'used_in_product_listing'       => $this->_getValue($attr, 'used_in_product_listing', 0),
        'used_for_sort_by'              => $this->_getValue($attr, 'used_for_sort_by', 0),
        'apply_to'                      => $this->_getValue($attr, 'apply_to'),
        'position'                      => $this->_getValue($attr, 'position', 0),
        'is_configurable'               => $this->_getValue($attr, 'is_configurable', 1),
        'is_used_for_promo_rules'       => $this->_getValue($attr, 'used_for_promo_rules', 0)
    ));
    return $data;
}
| improve this answer | |
0

The attribute you are looking for is apply_to, e.g.

'apply_to' => array('configurable'),
| improve this answer | |
  • Thanks, is there anything else I need to add to get this to work? Just adding that attribute to my config is not changing anything. – veilig Feb 25 '13 at 20:36
  • Be sure to remove the file from core_resource table and refresh the cache. It may be that the previous file has already been run and is stored inside that table – user1704524 Jan 30 '14 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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