3

I'm looking for a function that get all labels of a specific attribute. However the label that I get is the admin view. I would like to show the default store view instead.

Here is my current wrong code :

 $attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', '81');
 foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
    /*some stuff here*/
 }

Thanks for your help !

Bijal Bhavsar
  • 1,331
  • 10
  • 29
axelparatre
  • 125
  • 4
  • 16
  • how are you running this script? getAllOptions() will return the values based on the current scope (in your case looks like default/admin). – FlorinelChis Dec 13 '13 at 14:40
  • It's in a custom page in my design template directory. I don't know why it is set to default/admin though... – axelparatre Dec 13 '13 at 16:58

4 Answers4

5

you can use below code.

  <?php
  $productAttribute = $_product->getResource()->getAttribute($attr);
  $_label = $productAttribute->getStoreLabel(Mage::app()->getStore()->getName());
  echo $_label;
  ?>

The above code will give the attribute label as per store wise.

Keyul Shah
  • 7,219
  • 12
  • 37
  • 60
4

You can get all options with value using below code:

 $config    = Mage::getModel('eav/config');
    $attribute = $config->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'brand');// change 'brand' with your attribute code.    
    $values    = $attribute->getSource()->getAllOptions(); // first method to get attribute option values

Second Method:

$options = Mage::getResourceModel('eav/entity_attribute_option_collection');
$values  = $options->setAttributeFilter($attribute->getId())->setStoreFilter($storeId)->toOptionArray();

Now to get all labels you can use below code:

foreach($values as $option)
    {
        if(!empty($option['label'])){
         $_labels[] = $option['label'];
        }
    }
    echo '<pre>';
    print_r($_labels);

I hope this will help you :)

Bijal Bhavsar
  • 1,331
  • 10
  • 29
1

this will help you

// Here you can have whole model of perticular attribute.
$productAttribute = $_product->getResource()->getAttribute($attrCode);

// Now you can get it by store or can get all.
$_label = $productAttribute->getStoreLabel(Mage::app()->getStore()->getName());
echo $_label;
Sohil Desai
  • 193
  • 2
  • 9
0

Optimized Code:

PASS THE ATTRIBUTE ID AND OPTIONID TO THE below query - this will easily get the admin label of the corresponding attribute.

$attributeid = ''; // here pass the attributeid
$optionId = ''; //here pass the optionid


$query = "SELECT IF(tsv.value_id > 0, tsv.value, tdv.value) AS value FROM eav_attribute_option AS main_table
        INNER JOIN eav_attribute_option_value AS tdv ON tdv.option_id = main_table.option_id
        LEFT JOIN eav_attribute_option_value AS tsv ON tsv.option_id = main_table.option_id 
        AND tsv.store_id = 0 WHERE (attribute_id = {$attributeid}) AND (tdv.store_id = 0) and (tsv.option_id = {$optionId})";
Teja Bhagavan Kollepara
  • 3,816
  • 5
  • 32
  • 69
Nithin Ninan
  • 391
  • 2
  • 8