【问题标题】:Magento - Multi Select Category Attribute not savingMagento - 多选类别属性不保存
【发布时间】:2014-12-19 09:49:44
【问题描述】:

我正在使用 Magento 社区 1.9.0.0。

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

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

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

这是真的吗?多选不能在 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();

【问题讨论】:

  • Multiselect 绝对应该适用于类别属性。遵循本教程。它适用于 textarea,但您可以以类似的方式为下拉菜单创建一个。 atwix.com/magento/add-category-attribute
  • 你知道为什么它可能无法保存吗?
  • 您是否创建了源模型?在此处发布您的代码。
  • 上面添加了代码。我没有源模型。我会放什么?

标签: magento attributes categories


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    所以这个问题有点老了,但我认为迟到总比没有好。

    类别可以与多选一起出色地工作,您只需要帮助他们一点。多选作为数组发送到类别实体,它无法处理,这意味着您最终会得到一个空的多选。您想要做的是将 id 合并到一个字符串中。要么使用 javascript,要么像我一样,使用观察者。

    我的多选属性称为category_limit。

    这在config.xml

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2019-01-14
      • 1970-01-01
      相关资源
      最近更新 更多