Sunday, February 8, 2015

Get attribute options values and lables by attribute code

<?php
$myColorArr = myGetAttributeOptionArr('my_color');

echo '<pre>' . print_r($myColorArr, TRUE) . '</pre>';

function myGetAttributeOptionArr($attributeCode) {
  $outArr = array();
  $optArr = Mage::getSingleton('eav/config')->getAttribute('catalog_product', $attributeCode)->getSource()->getAllOptions();

  foreach ($optArr as $val) {
    if (empty($val['value']) || empty($val['label'])) {
      continue;
    }

    $outArr[$val['value']] = $val['label'];
  }

  return $outArr;
}
?>

No comments: