0

i'm tring to create a custom attribute only for a specific product. Into apply_to field i have setted a list "configurable, grouped" , a array('configurable', 'grouped') or single type, but doesn't work fine

what is the problem?

    <?php

$installer = $this;

$installer->startSetup();


$this->addAttribute('catalog_product', 'project_test3', array(
'type'              => 'text',  
'input'             => 'text',
'label'             => 'Project TEST3',
'backend'           => '',
'visible'           => true,
'required'          => false,
'visible_on_front'  => true,
'apply_to'          => 'configurable')); $this->endSetup(); ?>

2 Answers 2

0

Please check the below code to create a custom attribute that applies to configurable in Magento 2.

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'attribute_for_config',
        [
            'group' => 'Product Details',
            'type' => 'varchar',
            'backend' => '',
            'frontend' => '',
            'sort_order' => 50,
            'label' => 'Can be wrapped?',
            'input' => 'boolean',
            'class' => '',
            'source' => '',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'visible' => true,
            'required' => false,
            'user_defined' => false,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'unique' => false,
            'apply_to'=>'configurable'
        ]
    );
0

I'm guessing you're using the Mage_Eav_Model_Entity_Setup instead of Mage_Catalog_Model_Resource_Setup as your setup resource model. Because of that, you can't simply add the field apply_to directly in the attribute's creation.

If you can, just change (in your config.xml file) the class used as your resource in setup to Mage_Catalog_Model_Resource_Setup and you're done:

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

But if you really need to use Mage_Eav_Model_Entity_Setup as your setup, that's why you're unable to set a value to apply_to the way you wanted:

If you look into your installer class (app/code/core/Mage/Eav/Model/Entity/Setup.php) you will se that inside the method addAttribute() the data you sent as third parameter (attr) is passed to the method _prepareValues():

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) # Here your data is sent to be 'prepared' (including the 'apply_to' field)
         );
         ...
}

This "preparation method" applies some validation to values specified in the creation fields (like apply the default value if nothing is set to a field). The problem is: there's no mention of the apply_to field in there, so it get lost in the process:

protected function _prepareValues($attr)
    {
        $data = array(
            'backend_model'   => $this->_getValue($attr, 'backend'),
            'backend_type'    => $this->_getValue($attr, 'type', 'varchar'),
            'backend_table'   => $this->_getValue($attr, 'table'),
            'frontend_model'  => $this->_getValue($attr, 'frontend'),
            'frontend_input'  => $this->_getValue($attr, 'input', 'text'),
            'frontend_label'  => $this->_getValue($attr, 'label'),
            'frontend_class'  => $this->_getValue($attr, 'frontend_class'),
            'source_model'    => $this->_getValue($attr, 'source'),
            'is_required'     => $this->_getValue($attr, 'required', 1),
            'is_user_defined' => $this->_getValue($attr, 'user_defined', 0),
            'default_value'   => $this->_getValue($attr, 'default'),
            'is_unique'       => $this->_getValue($attr, 'unique', 0),
            'note'            => $this->_getValue($attr, 'note'),
            'is_global'       => $this->_getValue($attr, 'global', 1)
        );

        return $data;
    }

Because now the $data returned from this method will be used to actually create the new attribute (whithout the apply_to) field.

To fix that, there is two ways (that I can think of):

Change the _prepareValues() method

I would avoid to use this without further inspection. But you could easily add the apply_to to the end of the array inside de _prepareValues() method in app/code/core/Mage/Eav/Model/Entity/Setup.php:

protected function _prepareValues($attr)
    {
        $data = array(
            ...
            'apply_to' => $this->_getValue($attr, 'apply_to') # Added `apply_to` field
        );

        return $data;
    }

And now you will be able to use the apply_to on attribute's creation (like you've done before "apply_to" => "configurable".

Safest way

You can create the attribute and, after that, update it's apply_to value:

$attributeCode = "project_test3";
$this->startSetup();
# Create new attribute without the "apply_to" field
$this->addAttribute('catalog_product', $attributeCode, array(
    'type' => 'text',
    'input' => 'text',
    'label' => 'Project TEST3',
    'backend' => '',
    'visible' => true,
    'required' => false,
    'visible_on_front' => true,
));

# Loading new attribute and setting "apply_to" to "configurable"
$attribute = Mage::getModel('eav/entity_attribute')->loadByCode("catalog_product", $attributeCode);
$attribute->addData(array("apply_to" => "configurable"));
$attribute->save();

$this->endSetup();

Using this method you will not need to change any Magento core function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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