【问题标题】:Magento API: Set dropdown attribute option for a storeviewMagento API:为商店视图设置下拉属性选项
【发布时间】:2015-04-23 13:17:06
【问题描述】:

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

我找到了一个为默认商店视图创建下拉选项的函数:

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 添加一个新的属性值。

例子:

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

addAttributeOption("mycolor", "black")

现在我有一家德国商店的商店视图,并且喜欢设置德国颜色。我需要类似的东西

addAttributeOption("mycolor", "black", "schwarz", $storeview)

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

有人知道我该怎么做吗?

最好的问候

【问题讨论】:

  • 好的,没有办法的时候我会通过mysql查询解决。

标签: api magento


【解决方案1】:

我认为您已经找到了解决方案,但也许我可以帮助像我一样刚接触 Magento 的其他人。今天我必须找到一种方法,将属性(仅是产品属性)从外部产品管理系统导入到运行多个商店视图的 Magento 中。我不知道提问者的 addAttributeOption 函数来自哪里,但 Magento 安装程序脚本提供了它自己的 addAttributeOption()。所以我查看了 Setup.php,其中定义了 Magento 的 addAttributeOption():

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

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

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

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

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

如您所见,'value' 需要一个数组,而该数组的键决定了 storeID。在我在网上找到的大多数 addAttributeOption()-introductions 中,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”和“option2”。

你可以像这样调用 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多