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 !

2
  • how are you running this script? getAllOptions() will return the values based on the current scope (in your case looks like default/admin). Commented Dec 13, 2013 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... Commented Dec 13, 2013 at 16:58

4 Answers 4

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 :)

1
  • perfect the first method works for me ! Thank you so much for your help everyone ! Commented Dec 17, 2013 at 14:55
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.

1
  • Thank you for your answer but like I said I would like to get all the labels of a specific attribute. Commented Dec 16, 2013 at 8:27
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;
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})";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.