2014-12-19 82 views
0

我正在使用Magento Community 1.9.0.0。Magento - 多选择类别属性不保存

我已经以编程方式创建了一个多选的自定义分类属性。该属性可以在我的管理员类别部分中看到。当我选择任何值 - 无论是一个或多个,并按保存我得到一个成功的保存消息,但值永远不会保存。

然后,我也试着创建类别属性的扩展。这有同样的问题。当我联系支持时,他们说

类别实体不支持多选,因此这种类型的属性不适用于类别。

这是真的吗? CE-1.9上的多选不能用于类别属性?

这里是我使用编程创建它的代码:

require_once('app/Mage.php'); 
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID)); 
$installer = new Mage_Sales_Model_Mysql4_Setup; 
$attribute = array(
    'type' => 'text', 
    'label'=> 'Room Type', 
    'input' => 'multiselect', 
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
    'visible' => true, 
    'required' => false, 
    'user_defined' => true, 
    'default' => "", 
    'group' => "General Information", 
    'option' => array ( 
     'value' => array(
      'kitchen' => array('Kitchen'), 
      'bedroom' => array('Bedroom'), 
      'bathroom' => array('Bathroom'), 
      'loft' => array('Loft'), 
      'basement' => array('Basement'), 
      'lounge' => array('Lounge') 
     ) 
    ) 

); 
$installer->addAttribute('catalog_category', 'room_type', $attribute); 
$installer->endSetup(); 
+0

Multiselect应该可以使用类别属性。 请按照本教程。这是为textarea,但你可以以类似的方式为下拉菜单创建一个。 http://www.atwix.com/magento/add-category-attribute/ – Harit 2014-12-19 10:54:54

+0

你知道为什么它可能没有保存的任何理由吗? – 2014-12-19 10:55:51

+0

您是否创建了源模型?在这里发布你的代码。 – Harit 2014-12-19 10:56:22

回答

0

所以这个问题是有点老了,但我想迟到总比不到好。

类别可以与多个选择出色地工作,你只需要帮助他们一点点。多选被作为一个数组发送到类别实体,它不能处理,这意味着你最终会得到一个空的多选。你想要做的是将ID合并成一个字符串。要么使用JavaScript,要么像我一样,使用观察者。我的多重属性名为category_limit

这正好在​​3210

<events> 
    <catalog_category_prepare_save> 
     <observers> 
      <rianorie_categorylimit_save> 
       <class>rianorie_categorylimit/observer</class> 
       <method>adjustCategory</method> 
      </rianorie_categorylimit_save> 
     </observers> 
    </catalog_category_prepare_save> 
</events> 

,这是实际的观察者做的工作:

class RianOrie_CategoryLimit_Model_Observer 
{ 
    public function adjustCategory(Varien_Event_Observer $observer) 
    { 
     $event = $observer->getEvent(); 
     $category = $event->getCategory(); 
     $category->setCategoryLimit(
       implode(',', $category->getCategoryLimit()) 
     ); 
    } 
} 
0

@Rian是正确的,多选值作为数组发送的,其可以不会被处理。但是,我们不必使用观察者。相反,我们应该为该属性添加后端模型。 创建它时,将以下行添加到属性数据数组中。

'backend' => 'your_module/category_attribute_backend_related', 

在你的后端模式(Your_Module_Model_Category_Attribute_Backend_Related),你应该具备以下功能,帮助选定的选项保存或正确装入。

public function beforeSave($object) { 
    $attributeCode = $this->getAttribute()->getName(); 
    if ($attributeCode == 'room_type') { 
     $data = $object->getData($attributeCode); 
     if (!is_array($data)) { 
      $data = array(); 
     } 
     $object->setData($attributeCode, join(',', $data)); 
    } 
    if (is_null($object->getData($attributeCode))) { 
     $object->setData($attributeCode, false); 
    } 
    return $this; 
} 

public function afterLoad($object) { 
    $attributeCode = $this->getAttribute()->getName(); 
    if ($attributeCode == 'room_type') { 
     $data = $object->getData($attributeCode); 
     if ($data) { 
      $object->setData($attributeCode, explode(',', $data)); 
     } 
    } 
    return $this; 
}