无法在Magento上保存多选属性

时间:2013-11-07 02:17:42

标签: magento

我通过安装程序创建了自定义多选产品属性。如果我只从多选值中选择一个选项,它可以保存产品。但是,如果我选择2个值,产品仍然可以保存,但会再次返回1个选定值。简而言之,我无法使用2个选定值保存属性。

$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();

5 个答案:

答案 0 :(得分:7)

问题来自您的属性类型。

'type'=> 'int',

多选属性的值由逗号1,4,6连接保存。为此,您需要将属性设为varchartext。如果您不打算为该属性提供数百个选项,我建议varchar 现在配置方式,当它保存时,值1,4,6将转换为int,最终为1

答案 1 :(得分:1)

修改选项数组
 'option' => array (
  'value' => array(
  '0' => array('First Option'),
  '1' => array('Second Option'),
  '2' => array('Third Option'),
  )
),

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

),

Multiselect将接受相关的数组。

答案 2 :(得分:0)

我在自定义类别属性中遇到了问题。它不会将多选值保存在数据库中,也不会在类别后端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


        ));
        ?>

这是我的帮助文件代码

<?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;
    }
}  ?>

答案 3 :(得分:0)

    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...**

答案 4 :(得分:0)

我正在使用SOAP API在magento商店中输入产品。这是完整的代码

在多选自定义属性的情况下。

            $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)))
            ));