1

Working in a product view template (.phtml) I am trying to get the Admin label for a configurable option value. I have managed to var_dump the front end values:

$productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);

$attributeOptions = array();

foreach ($productAttributeOptions as $productAttribute) {

    foreach ($productAttribute['values'] as $attribute) {

        $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
    }
}

var_dump($attributeOptions);

This gives me an array:

array (size=1)
  'Configurable Option' => 
    array (size=2)
      378 => string 'Black' (length=5)
      379 => string 'Pink' (length=4)

Which is all fine and dandy. But how would I go about getting the "Admin" values for these options? The reason I would like to do this is because the admin values will contain hex colours that I can use to show a colour for a visitor to select instead of just having a drop down.

Any help is muchly appreciated.

2 Answers 2

0

As you have using this at frontend,then you cannot easy get those option. Get need to use option collection and filter the option collection by Attribute id and option ids.

Get Option ids:

Get option list from your code:

    $productAttributeOptions = $_product->getTypeInstance(true)->getConfigurableAttributesAsArray($_product);

$attributeOptions = array();
$attributeList=array();
foreach ($productAttributeOptions as $productAttribute) {


    foreach ($productAttribute['values'] as $attribute) {
        $attributeList[$productAttribute['attribute_id']][]=$attribute['value_index'];

        $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
    }
}

get Admin label:

then call eav option collection then filter collection by option is

 foreach( $attributeList as $key=>$value){
    $attributeId=$key;
    $options=array_unique($value);
            $collection = Mage::getResourceModel('eav/entity_attribute_option_collection');
                $collection->getSelect()->where('main_table.option_id IN (?)',$options);
                $collection->setPositionOrder('asc');
                $collection->setAttributeFilter($attributeId)
                ->setStoreFilter(0);
            $collection->load();
echo "<pre>";
print_r($collection->toOptionArray());

}

and 0 for store admin

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, privacy policy and cookie policy

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