Magento API:设置商店视图的下拉属性选项

时间:2015-04-23 13:17:06

标签: api magento

我正在使用magento API,需要为不同的商店内容创建下拉选项。

我找到了一个为默认storeview创建下拉选项的函数:

public function addAttributeOption($arg_attribute, $arg_value) 
{   
    $attribute_model = Mage::getModel('eav/entity_attribute'); 
    $attribute_options_model= Mage::getModel('eav/entity_attribute_source_table');   
    $attribute_code = $attribute_model->getIdByCode('catalog_product', $arg_attribute); 
    $attribute = $attribute_model->load($attribute_code);   
    $attribute_table = $attribute_options_model->setAttribute($attribute); 
    $options = $attribute_options_model->getAllOptions(false);   
    $value['option'] = array($arg_value,$arg_value); 
    $result = array('value' => $value); 
    $attribute->setData('option',$result); 
    $attribute->save();   
}

这个功能很好,我可以为默认的storeview添加一个新的attribut值。

示例:

我有属性“mycolor”并调用函数

addAttributeOption("mycolor", "black")

现在我有一个德国商店的商店视图,喜欢设置德国颜色。我需要像

这样的东西

addAttributeOption(“mycolor”,“black”,“schwarz”,$ storeview)

表示将storeview的颜色选项设置为schwarz,默认值的颜色为黑色。

有人有想法我该怎么做?

祝你好运

1 个答案:

答案 0 :(得分:3)

我认为你找到了解决方案,但也许我可以帮助像我这样对Magento不熟悉的其他人。今天,我必须找到一种方法将属性(仅限产品属性)从外部Products-Managing-System导入到运行多个商店视图的Magento中。我不知道提问者的addAttributeOption函数来自哪里,但Magento安装程序脚本提供了自己的addAttributeOption()。所以我看了一下Setup.php,其中定义了Magento的addAttributeOption():

  

{你的Magento Path} /app/code/core/Mage/Eav/Model/Entity/Setup.php

现在,在使用(1.9.1.0)的Magento版本中,addAttributeOption()需要一个参数,一个名为$ option的数组。它的架构如下:

Array (
    'attribute_id'  => '{attributeId}',
    'value'         => array(
        '{optionId}'    => array(

            '{storeId}'    => '{labelName}',

        ),
    ),
    'delete'        =>  array(
        //...
    ),
    'order'         => array(
        //...
    )
);

正如您所看到的,'价值'期望一个数组和这个数组的键确定storeID。在大多数addAttributeOption() - 我在网上找到的介绍中,storeID被硬编码为0而没有进一步的解释 - 0使其成为必需的默认管理值。因此,很明显,现在,为了使用依赖于StoreView的标签添加选项,我们只需为每个StoreView添加一个额外的数组值,如下所示:

Array (
    'attribute_id'  => $attribute_id,
    'value'         => array(
        'option1'   => array(

            '0'    => 'black',              // required admin value
            '1'    => 'Schwarz (RAL 9005)', // if storeId = 1 is German
            '2'    => 'Black (RAL 9005)',   // if storeId = 2 is English

        ),
        'option2'   => array(

            '0'    => 'blue',
            '1'    => 'Blau (RAL 5015)',
            '2'    => 'Blue (RAL 5015)',

        ),
        // And so on...
    )
);

注意:如果您的选项的数组索引是一个数字,则addAttributeOption()期望它是已存在选项的ID号。如果您想要更新现有选项,这非常有用,但这也意味着一个新的选项仍然是数字。因此,我将它们命名为' option1' &安培; '选项2'

您可以像这样调用addAttributeOption():

Mage::app();
$installer = Mage::getResourceModel('catalog/setup','catalog_setup');
$installer->startSetup();

// ...
// generate your Options-Array
// I called it $newOptions

$installer->addAttributeOption($newOptions);

$installer->endSetup();