2011-10-24 12 views
3

私は次の問題があります。特別なモジュールでは、属性のオプションをその位置に基づいてソートする必要があります。属性のオプションを取得しようとすると、IDとラベルを取得できますが、そのオブジェクトには何もありません。MagentoでAttribute-Optionの位置を取得するにはどうすればよいですか?

Iは、例えば、これを行うことができます。

$attribute = $_product->getResource()->getAttribute($code)->getOptionsText($value): 

それともgetOptionId($値)が、バックエンドで編集可能な位置を取得するものは何もありません。だから、これをどうやって得るのですか?まだネット上で何か(有用)を見つけられませんでした。

(また、同様の質問magento sort attribute option collection by position? doesntのは、任意の助けを与える)

EDIT:そこに、

SELECT sort_order FROM mag_eav_attribute_option WHERE option_id = 114 AND attribute_id = 533; 

しかし、私は思う: は私が行うために管理何、このような直接的なSQL文を、やっていますその価値を得るためのより良い選択肢です。

答えて

1

私は答えを自分で見つけ、あなたとそれを共有したいです。

$attribute = Mage::getModel('eav/entity_attribute')->load($code, 'attribute_code'); 
$option_col = Mage::getResourceModel('eav/entity_attribute_option_collection') 
->setAttributeFilter($attribute->getId()) 
->setStoreFilter() 
->setPositionOrder('ASC'); 
$option_col->getSelect()->order('main_table.sort_order '.$orderby); 

私はそれが誰かを助けてくれることを願っています。

+1

チャームのように働いた。驚くばかり。 –

0

スタートは、デバッグに:

print_r($_product->getResource()->getAttribute($code)); 
print_r($_product->getResource()->getAttribute($code)->getData()); 
print_r(get_class_methods($_product->getResource()->getAttribute($code))); 
+0

もちろん私はすでにそれをしました。私が言ったように、属性自体はgetOptionIdとgetOptionTextしか持っていませんが、位置はありません。 –

0

から:http://www.phptechi.com/getting-product-attributes-values-and-labels.html

属性値のソート順を取得する方法?

ここでは、SQLを使用してアトリビュート値の並べ替えを迅速かつ簡単に行う方法を示します。コードは次の変数に対する 変更値:

$ CurtAtrは、色、サイズなどの現在の属性である、など
$てattrValは、例えば、属性値でありますサイズがあり、「小、中、大、など」

$resource = Mage::getSingleton('core/resource'); 
$read = $resource->getConnection('catalog_read'); 
$read->fetchAll("SELECT ao.sort_order FROM eav_attribute_option_value as aov, eav_attribute_option as ao where value='$attrVal' and aov.option_id = ao.option_id and attribute_id=$CurtAtr limit 1"); 
1

私はあなたがすでに似た何かを思い付い見るあなたがMage_Eav_Block_Adminhtml_Attribute_Edit_Options_Abstract

$attributeId = 230; 
$options = Mage::getResourceModel('eav/entity_attribute_option_collection') 
      ->setAttributeFilter($attributeId) 
      ->setPositionOrder('desc', true) 
      ->load(); 

foreach($options as $opt){ 
    Mage::log($opt->getSortOrder()); 
} 

@見てみましょうが、私は私が思った場合、これは実際には非常に簡単ですそれは他人に役立つかもしれないので、これを投稿するでしょう。

+1

+1あなたのソリューションがループに$ opt-> getData()を追加できるようにしたいと思っていました。それぞれの位置にもlableがあります – Nickool

0

あなたの属性コードは$codeで、属性オプション値は$valueです。次に、それに対応するソート順を得ることができます:

$source = $product->getResource()->getAttribute($code)->getSource(); 
$optionId = $source->getOptionId($product->getData($code)); 
$optionSortOrder = Mage::getResourceModel('eav/entity_attribute_option_collection') 
        ->setIdFilter($_optionId) 
        ->getFirstItem() 
        ->getSortOrder(); 
関連する問題