2016-12-23 17 views
0

私は以下のコードを使用して、attribute set "size"のオプションIDを取得しています。 オプションラベルが9より大きい場合(つまり10,12,13,14 ..)、正しいIDを返しています一部のラベルでmagento getOptionIdが動作しない

オプションラベルが10未満の場合(つまり5,6,7 、8,9)。

$attr = 'size'; 
$attribute_label = 13; 
$_product = Mage::getModel('catalog/product'); 
$attribute_name = $_product->getResource()->getAttribute($attribute_name); 

if ($attr->usesSource()) { 
    echo $color_id = $attribute_name->getSource()- >getOptionId($attribute_label); 
} 

例:

出力私は13それ 戻り5.

出力Iは、オプションのラベルを使用する場合($ attribute_label =とオプションのラベルを($ attribute_label = 13)を使用します6)は6として返す 6.

+0

をあなたは以下試すことができます記事 –

答えて

0

これは、getこの質問にも述べたようにOptionId()は、数値ラベルでうまく動作しません:あなたは何ができるかhttps://magento.stackexchange.com/questions/128445/getoptionid-method-returns-invalid-option-id

のようなもの、このメソッドを使用して回避してコードを調整することです:

$attribute_name = 'size'; 
$attribute_label = 13; 
$optionId = false; 
$attr = Mage::getResourceModel('catalog/product')->getAttribute($attribute_name); 
if ($attr->usesSource()) { 
    $options = $attr->getSource()->getAllOptions(); 
    foreach($options as $option) { 
     if($option['label'] == $attribute_label) { 
      $optionId = $option['value']; 
      break; 
     } 
    } 
} 
関連する問題