user1801605
user1801605

Reputation: 441

Can't save multiselect attribute on Magento

I created a custom multiselect product attribute through installer. It works and I can save the product if I only select one option from the multiselect values. But if I select 2 values, the product still can be saved but came back up with 1 selected value again. In short, I can't save the attribute with 2 selected values.

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

$installer->addAttribute('catalog_product', 'attr_id',array(
         'label'             => 'Frontend Name',
         'type'              => 'int',
         'input'             => 'multiselect',
         'backend'           => 'eav/entity_attribute_backend_array',
         'frontend'          => '',
         'source'            => '',
         'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
         'visible'           => true,
         'required'          => false,
         'user_defined'      => true,
         'searchable'        => false,
         'filterable'        => false,
         'comparable'        => false,
         'option'            => array (
            'value' => array(
                     '0' => array('First Option'),
                     '1' => array('Second Option'),
                     '2' => array('Third Option'),
                     )
                    ),
         'visible_on_front'  => false,
         'visible_in_advanced_search' => false,
         'unique'            => false
));

$installer->endSetup();

Upvotes: 3

Views: 9736

Answers (5)

user3146094
user3146094

Reputation: 429

I have faced a problem in my custom category attribute. It does not save the multiselect value in data base and does not show multiselect values on the category backend admin.

        <?php
        require_once("app/Mage.php");
        Mage::app('default');
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

        $installer = new Mage_Eav_Model_Entity_Setup('core_setup');
        $entityTypeId     = $installer->getEntityTypeId('catalog_category');
        $attributeSetId   = $installer->getDefaultAttributeSetId($entityTypeId);
        $attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

        $installer->addAttribute('catalog_category', 'cutomcity',  array(
             'label'            => 'Test Select',
            'type'              => 'varchar',
            'input'             => 'multiselect',
            'visible'           => true,
            'user_defined'      => true,
             'required'         => false,
            'position'          => 80,
            'visible_on_front'  => false,    

            'group'    => 'General Information',   
            'input'    => 'multiselect', 
    'backend_model'=>'eav/entity_attribute_backend_array'   

            'source' => 'GA_Multiattribute_Helper_Testsource'


        // eav/entity_attribute_source_table   Even if i use this untill it does not show selected value on the Multiselect


        ));
        ?>

This is my helper file code

<?php 

class GA_Multiattribute_Helper_Multiattributesource extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    protected $_optionsDefault = array();
    public function getAllOptions($withEmpty = true, $defaultValues = false)
    {
        $collection = Mage::getModel('customer/customer')->getCollection()->addAttributeToSelect('*');;
        $customers = array();
        foreach($collection as $cust)
        {
            $fname = $cust->getFirstname();
            $lname = $cust->getLastname();
            $id = $cust->getId();
            $customers[] = array('value'=>"$id$fname", 'label'=>"$fname $lname");
        }
        return $customers;
    }
}  ?>

Upvotes: 0

Rebin N Shaju
Rebin N Shaju

Reputation: 21

I am using SOAP API for entering products in magento shops. here is the full code

In the case of multiselect custom attribute.

            $arrProductTime = explode(',', '136,139');

            $result = $client->catalogProductCreate($session, 'simple', $attributeSet->set_id, 'product_sku1234', array(

                'categories' => array(36),
                'websites' => array(1),
                'name' => 'my_pdt1008',
                'description' => 'my_pdt1',
                'short_description' => 'my_pdt1000',
                'weight' => '11',
                'status' => '1',
                'url_key' => 'product-url-key1',
                'url_path' => 'product-url-path1',
                'visibility' => '4',
                'price' => '100',
                'tax_class_id' => 1,
                'meta_title' => 'Product meta title1',
                'meta_keyword' => 'Product meta keyword1',
                'meta_description' => 'Product meta description1',
                'stock_data' => array('qty'=>'100','is_in_stock'=>1,'manage_stock'=>1),
                'additional_attributes' => array('multi_data' => array(array('key' => 'product_time', 'value' => $arrProductTime)))
            ));

Upvotes: 0

user3146094
user3146094

Reputation: 429

    I have found the solution myself .

    open app/code/core/Mage/Adminhtml/controllers/Catalog/CategoryController.php 
    on save action after this

    $category->setAttributeSetId($category->getDefaultAttributeSetId());

    Please change language is your attribute name . you can change attribute name accordingly

    $ga = "";
    if($data['general']['language']){
    foreach($data['general']['language'] as $a){
    $ga .= $a.",";
    }
    $category->setLanguage(substr_replace($ga, "", -1));

    }
Please replace language to your attribute name and it works...**

Upvotes: 0

Marius
Marius

Reputation: 15206

The problem comes from the type of your attribute.

'type'=> 'int',

The values from multiselect attributes are saved concatenated by comma 1,4,6. For this you need the attribute to be varchar or text. I recommend varchar if you are not going to have hundreds of options for the attribute.
The way is configured now, when it's saved, the value 1,4,6 is converted to int and it ends up being 1.

Upvotes: 7

Deependra Singh
Deependra Singh

Reputation: 1514

Modify you option array from

 'option' => array (
  'value' => array(
  '0' => array('First Option'),
  '1' => array('Second Option'),
  '2' => array('Third Option'),
  )
),

to

'option' => array (
  'value' => array(
  'first_option' => array('First Option'),
  'second_option' => array('Second Option'),
  'third_option' => array('Third Option'),
  )

),

Multiselect will accept associated array.

Upvotes: 1

Related Questions