0

i am trying to insert some attributes (drop-down) into the magento. The creation of the attribute with option is completed successfully. I also create an attribute set based on that attribute successfully. The problem is that when i add a product based on that attribute set the product is not visible on the website.

Here is my configuration for the dropdown attribute:

    $data = array(
                    'is_global'                     => '1',
                    'frontend_input'                => 'select',
                    'default_value_text'            => '',
                    'default_value_yesno'           => '0',
                    'default_value_date'            => '',
                    'default_value_textarea'        => '',
                    'is_unique'                     => '0',
                    'is_required'                   => '1',
                    'frontend_class'                => '',
                    'is_searchable'                 => '0',
                    'is_visible_in_advanced_search' => '0',
                    'is_comparable'                 => '0',
                    'is_used_for_promo_rules'       => '0',
                    'is_html_allowed_on_front'      => '1',
                    'is_visible_on_front'           => '0',
                    'used_in_product_listing'       => '0',
                    'used_for_sort_by'              => '0',
                    'is_configurable'               => '1',
                    'is_filterable'                 => '0',
                    'is_filterable_in_search'       => '0',
                    'backend_type'                  => 'varchar',
                    'default_value'                 => '',
                    'option'                        => ''
                );

--

i 've changed the above array to this one:

    $data = array(

                    'is_global'                     => true,
                    'frontend_input'                => 'select',
                    'is_required'                   => true,
                    'visible'                       => true,
                    'is_html_allowed_on_front'      => true,
                    'is_visible_on_front'           => true,
                    'is_configurable'               => true,
                    'is_filterable'                 => true,
                    'backend_type'                  => 'varchar',
                    'apply_to'                      => array('simple'),
                    'default_value'                 => '',
                    'position'                      => 1,
                    'visible_on_front'              => true,
                    'option'                        => ''
                );

but i still have the same problem. i think that i am missing something here.

1

Here is what I used recently in my project and successfully created it programatically. I see that your apply_to is using array, though I think you don't need array and can pass multiple product types comma-separated.

$model = Mage::getResourceModel('catalog/setup','catalog_setup');

$data=array(
'type'=>'int',
'input'=>'boolean', //for Yes/No dropdown
'sort_order'=>50,
'label'=>'CUSTOM ATTRIBUTE CODE LABEL',
'global'=>Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
'required'=>'0',
'comparable'=>'0',
'searchable'=>'0',
'is_configurable'=>'1',
'user_defined'=>'1',
'visible_on_front' => 0, //want to show on frontend?
'visible_in_advanced_search' => 0,
'is_html_allowed_on_front' => 0,
'required'=> 0,
'unique'=>false,
'apply_to' => 'configurable', //simple,configurable,bundled,grouped,virtual,downloadable
'is_configurable' => false
);

$model->addAttribute('catalog_product','CUSTOM_ATTRIBUTE_CODE',$data);

$model->addAttributeToSet(
    'catalog_product', 'Default', 'General', 'CUSTOM_ATTRIBUTE_CODE'
); //Default = attribute set, General = attribute group
1
  • i 've tried both of them. in either way this option works great. i think that the problem is with the 'is_configurable' option because when i edit the option manually after the creation everything works fine. Sep 19 '14 at 20:27
0

Update - To show on frontend use visible_on_front

If you take a look at /app/code/core/Mage/Catalog/Model/Resource/Setup.php

/**
 * Add attribute to an entity type
 *
 * If attribute is system will add to all existing attribute sets
 *
 * @param string|integer $entityTypeId
 * @param string $code
 * @param array $attr
 * @return Mage_Eav_Model_Entity_Setup
 */
public function addAttribute($entityTypeId, $code, array $attr)
{
    $entityTypeId = $this->getEntityTypeId($entityTypeId);
    $data = array_merge(
        array(
            'entity_type_id' => $entityTypeId,
            'attribute_code' => $code
        ),
        $this->_prepareValues($attr)
     );

$this->_prepareValues($attr) will set is_visible_on_front from visible_on_front

/**
 * Prepare catalog attribute values to save
 *
 * @param array $attr
 * @return array
 */
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;
}

Try adding to your array option

 array(
  ....  
    'visible_on_front'  => 1,
  ....
 )

You can either :

Remove and re-add using

// Remove Product Attribute
$installer->removeAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code');

Or Update

$installer->updateAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code', array(
    'visible_on_front'  => 1,
));

(assuming that your installer is of type Mage_Catalog_Model_Resource_Setup)

<?xml version="1.0"?>
<config>
 .....
    <models>
      <attr>
        <class>MagePal_Attr_Model</class>
        <resourceModel>attr_mysql4</resourceModel>
      </attr>
    </models>
    <resources>
      <productattr_setup>
        <setup>
          <module>MagePal_Attr</module>
          <class>Mage_Catalog_Model_Resource_Setup</class>
        </setup>
        .......
    </resources>
  </global>
</config> 

If not, then in your mysql4-install-*.php file add

<?php

$installer = $this;

$installer->startSetup();
$setup = new Mage_Catalog_Model_Resource_Setup('core_setup');
//$setup->removeAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code');
$setup->addAttribute(...);
7
  • i 've tried this and still not working. i think that the problem is with the 'is_configurable' attribute of the model. When i edit the attribute and save it again from backend eveyrthing works fine. Sep 19 '14 at 19:23
  • Take a look at my update Sep 19 '14 at 20:09
  • i 've checked that also but is not working also. which sql tables are used to store dropdown attributes so i can compare with a working attribute (created from the backend)? Sep 19 '14 at 20:54
  • How are you creating the attribute.. Please add your config.xml. $installer = new Mage_Catalog_Model_Resource_Setup('core_setup'); $installer->removeAttribute(Mage_Catalog_Model_Product::ENTITY, 'product_attribute_code'); Sep 19 '14 at 20:59
  • Also take a look at stackoverflow.com/questions/10842555/… Sep 19 '14 at 20:59
0

the correct array with the attributes is the following:

    $data = array(

                    'is_global'                     => true,
                    'frontend_input'                => 'select',
                    'is_required'                   => true,
                    'visible'                       => true,
                    'is_html_allowed_on_front'      => true,
                    'is_visible_on_front'           => true,
                    'is_configurable'               => true,
                    'configurable'                  => true,
                    'is_configurableSpecified'      => true,
                    'is_visible_on_frontSpecified ' => true,
                    'is_filterable'                 => true,
                    'apply_to'                      => 'simple',
                    'default_value'                 => '',
                    'position'                      => 1,
                    'visible_on_front'              => true,
                    'option'                        => '',
                    'backend_type'                  => 'int'
                );

it seems that the 'backend_type' attribute should be set to 'int' to setup the attribute as it should be.

thanks

0

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.