【问题标题】:Magento - Adding Attributes programatically with default and searchableMagento - 以编程方式添加属性,默认和可搜索
【发布时间】:2013-06-09 20:19:54
【问题描述】:

我到处寻找答案,在 Magento 中一头雾水,但找不到可行的解决方案。我正在动态创建 magento 属性,这很好,但是当涉及到

  • 设置值
  • 设置默认值
  • 添加更多选项
  • 使属性可搜索

似乎没有任何效果。

这是我添加属性的代码

$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->addAttribute('catalog_product', $name, array(
    'type'       => 'varchar',
    'input'      => 'select',
    'backend'           => '',
    'frontend'          => '',
    'label'             => $key,                                                    
    'class'             => '',
    'source'            => '',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,
    'required'          => false,
    'user_defined'      => true,
    'default'           => '',
    'searchable'        => true,
    'filterable'        => true,
    'comparable'        => true,
    'visible'      => true,
    'visible_on_front'   => true,
    'visible_in_advanced_search'   => true,                                                 
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false,
    'option'        => array(
        'values'    => array($specific)
    )
));
$installer->endSetup();

$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key))->save();

添加很好,它甚至为我添加了选项,但我似乎无法将该选项设置为默认选项(稍后添加更多),我无法使其可搜索。

我真的希望有人可以提供帮助。

谢谢

更新:

好的,我已经设法让它使用此代码添加默认值(仍然不可搜索等)。

$key = "Brand";
$name = "brand";
$specific = "Cola";
$installer->startSetup();
$installer->addAttribute('catalog_product', $name, array(
    'type'       => 'int',
    'input'         => 'select',
    'backend'           => '',
    'frontend'          => '',
    'label'             => $key,                                                    
    'class'             => '',
    'source'            => 'eav/entity_attribute_source_table',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'visible'           => true,    
    'required'          => false,
    'user_defined'      => true,
    'default'           => '',
    'searchable'        => true,
    'filterable'        => true,
    'comparable'        => true,
    'visible'      => true,
    'visible_on_front'   => true,
    'visible_in_advanced_search'   => true,                                                 
    'unique'            => false,
    'apply_to'          => '',
    'is_configurable'   => false,
    'option'        => array(
        'value' => array(
                $this->getAttributeName($specific) => array($specific)
        )
    )
));
$installer->endSetup();

$attrID = $installer->getAttribute('catalog_product', $name,'attribute_id');
$attr = Mage::getModel('eav/entity_attribute')->load($attrID);
$attr->setStoreLabels(array(1 => $key));
$attr->setDefaultValue($attr->getSource()->getOptionId($this->getAttributeName($specific)));

$attr->save();

但是,当我使用此代码添加新选项时,其中 $specific = "Pepsi";

$model = Mage::getModel('catalog/resource_eav_attribute');

$option = array();

$option['attribute_id'] = $attr;                                                        
$option['value'][$this->getAttributeName($specific)][1] = $specific;

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);

我收到错误:“未定义默认选项值”

【问题讨论】:

    标签: magento plugins options addattribute


    【解决方案1】:

    您是否尝试过使用'is_searchable' => true 而不是'searchable' => true

    【讨论】:

      【解决方案2】:

      查看 catalaog_eav_attribute。有一列'is_searchable',类型为smallint

      试试这个:

      'is_searchable' => 1,
      

      【讨论】:

        【解决方案3】:

        参考错误:

        “未定义默认选项值”

        当你添加选项时:---------------------------> /

        $option['value'][$this->getAttributeName($specific)][1] = $specific;

        这里的 [0] 是选项的“默认管理值”,[1] 是“默认存储值”。 $option['value'][$this->getAttributeName($specific)][0] = $specific;

        您的代码没有将 $specific 设置为默认选项,而是将 $specific 设置为默认存储的值。

        【讨论】:

          猜你喜欢
          • 2012-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-01
          • 2018-09-11
          相关资源
          最近更新 更多