2013-04-02 20 views
7

私はしばらくの間フロントエンドマジェントをやっていましたが、ただモジュールを構築し始めました。これは私がフロントエンドを行う方法を知っている何かですが、私は自分のモジュールで苦労しています。私が現在達成しようとしているのは、利用可能なすべてのプロダクト属性を持つ管理者のマルチセレクションです。すべての製品属性セットにカスタム製品属性を含める。フラット・カテゴリー・データが有効であると仮定したくないので、どのテーブルが必要なのかは完全にはわかりません。すべての製品属性のリストをmagentoで取得

システム設定の新しいタブで管理領域を作成しましたが、現在は3つの静的オプションが設定されている複数選択フィールドが作成されています。これは多くの作品。誰かが正しい方向に指を指して私を助けることができます...現在これは私がこれまで持っていたものです(価値があるもののためです)。

<?php 
     class test_test_Model_Source 
     { 
      public function toOptionArray() 
      { 
       return array(
        array('value' => 0, 'label' =>'First item'), 
        array('value' => 1, 'label' => 'Second item'), 
        array('value' => 2, 'label' =>'third item'), 

       ); 
      } 
     } 

///////////////////////////// EDIT ///////////// ////////////////////////

私はここに何かがあるように感じますが、すべての属性の最初の文字を返すだけですそのさえ属性はそのが戻った場合、私は

public function toOptionArray() 
{ 
    $attributes = Mage::getModel('catalog/product')->getAttributes(); 
    $attributeArray = array(); 
    foreach($attributes as $a){ 

      foreach($a->getSource()->getAllOptions(false) as $option){ 
       $attributeArray[$option['value']] = $option['label']; 
      } 

    } 
    return $attributeArray; 
} 

)わからないんだけど////////////////////////////// /// EDIT /////////////////////////

私は極端ではない私はそれがすべてのattribute_codesに、私はそれが欲しいものを返す配列を知っているように閉じる。しかし、それはまだそれぞれの最初の文字を出力している...誰でも知っている理由は?

public function toOptionArray() 
{ 
    $attributes = Mage::getModel('catalog/product')->getAttributes(); 
    $attributeArray = array(); 

    foreach($attributes as $a){ 
     foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) { 
      $attributeArray[$attributeName] = $attributeName; 
     } 
     break;   
    } 
    return $attributeArray; 
} 

答えて

6

私は自分の質問に答えました。私はしかし、私はなぜ、誰かがコメントし、それが有用であることを説明することができた場合、私は確信していない方法を発見した。したがって、$ attributeArray [$ attributeName] = $ attributeName;配列を返したときにprint_rに来たときに働いていました。しかし、私の意見では、まったく同じことをしているように見える次のことをするなら、それは機能します。レンダリング時に文字列を期待するのではなく、何か他のものがあると想像することができます。とにかく、コードは次のとおりです:

public function toOptionArray() 
{ 
    $attributes = Mage::getModel('catalog/product')->getAttributes(); 
    $attributeArray = array(); 

    foreach($attributes as $a){ 

     foreach ($a->getEntityType()->getAttributeCodes() as $attributeName) { 

      //$attributeArray[$attributeName] = $attributeName; 
      $attributeArray[] = array(
       'label' => $attributeName, 
       'value' => $attributeName 
      ); 
     } 
     break; 
    } 
    return $attributeArray; 
} 
1

あなたは、あなたがこの方法でオプションを取得できる属性(Magentoのコードからコピー)

$result = array(); 
foreach($attributes as $attribute){ 
foreach ($attribute->getProductAttribute()->getSource()->getAllOptions() as $option) { 
    if($option['value']!='') { 
     $result[$option['value']] = $option['label']; 
    } 
} 

を持っていたら、この

$attributes = Mage::getSingleton('eav/config') 
->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getAttributeCollection(); 

のように、他の方法で属性を取得しようとすることができます}

+0

上記の編集を参照してください^^ –

+0

あなたは配列のprint_rの一部を投稿できますか? – Mat

+0

だから私はそれを返す二foreachループの$ attributeNameの中にログイン:: MAGE場合:allow_message DEBUG(7):allow_open_amount DEBUG(7):AW_commission_group DEBUG(7):(7) DEBUG box_depth DEBUG( 7):box_height DEBUG(7):box_weight DEBUG(7):box_width DEBUG(7):ブランド DEBUGは(7): DEBUG(7)can_be_gift_wrapped:category_ids を..... しかし、私が配列を返すとき、multiselectオプションは各属性の最初の文字を表示します。コード –

3

フランククラークが示唆したように、追加のループを行う必要はありません。ただ使用する:

public function toOptionArray() 
{ 
    $attributes = Mage::getResourceModel('catalog/product_attribute_collection')->addVisibleFilter(); 
    $attributeArray = array(); 

    foreach($attributes as $attribute){ 
      $attributeArray[] = array(
       'label' => $attribute->getData('frontend_label'), 
       'value' => $attribute->getData('attribute_code') 
      ); 
    } 
    return $attributeArray; 
} 
+0

これは受け入れられた答えでなければなりません。 – phpguru

関連する問題