2015-04-23 69 views
0

我正在使用magento API,並需要爲不同的商家視圖創建下拉選項。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") 

功能現在我有一個德國的店storeview,喜歡設置德語色彩。我需要這樣的東西

addAttributeOption(「mycolor」,「黑」,「施瓦茨」,$ storeview)

來設定storeview對施瓦茨的顏色選項,其中默認值的顏色爲黑色。

有沒有人有一個想法我該怎麼做?

問候

+0

好了,當沒有辦法,我將通過MySQL查詢解決。 – dermold

回答

1

我想你alreay找到你的解決方案,但也許我可以幫助別人誰是新的Magento像我。今天,我必須找到一種方法,即將外部產品管理系統的屬性(僅限於產品屬性)導入運行Magento的多個商店視圖。我不知道提問者的addAttributeOption函數來自哪裏,但Magento安裝程序腳本提供了它自己的addAttributeOption()。所以,我接過來一看進入Setup.php其中Magento的addAttributeOption()的定義是:

{你的Magento路徑} /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 ()期望它是已經存在的Option的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();