Get all attribute options

To get all attribute options from a Magento dropdown attribute (select field) you can use the following snippet:

<?php
$oAttibute = Mage::getModel('eav/config')->getAttribute(Mage_Catalog_Model_Product::ENTITY, 'sheldon_special');
$aOptions = $oAttibute->getSource()->getAllOptions();
var_dump($aOptions);

That gives you an array like that:

<?php
$aOptions = {array} [6]
 0 = {array} [2]
  label = ""
  value = ""
 1 = {array} [2]
  label = "best"
  value = "306"
 2 = {array} [2]
  label = "new"
  value = "305"
 3 = {array} [2]
  label = "no"
  value = "304"
 4 = {array} [2]
  label = "sale"
  value = "307"
 5 = {array} [2]
  label = "special"
  value = "632"

This can be used as source for a system config field for example. See my blogpost about Category dropdown element.

To get a specific value by given label, you can use this function:

<?php
/**
 * Returns option id for given value or false if it cannot be determined.
 *
 * Pattern:
 * $aAttributeOptions = [
 *  0 => [
 *      'label' => 'LABELTEXT',
 *      'value' => '123'
 *  ]
 * ];
 *
 * @param array   $aAttributeOptions Option array (
 *
 * @param  string $sLabel
 *
 * @return bool|string
 */
protected function _getOptionValueByLabel($aAttributeOptions, $sLabel)
{
    foreach ($aAttributeOptions as $aOption) {
        if (isset($aOption['label']) && $aOption['label'] == $sLabel) {
            return $aOption['value'];
        }
    }

    return false;
}

To get all attribute labels for each store into a CSV file, you can do something like that:

<?php
    /**
     * Exports Attribute store labels for multiselect and dropdown product
     * attributes
     *
     * @throws \Mage_Core_Exception
     */
    protected function _exportAttributeStoreLabels()
    {
        $sFile = $this->_getExportFilePath(self::ATTRIBUTE_LABEL_EXPORT_SUFFIX);
        $rFileHandle = fopen($sFile, 'w+');
        if (!$rFileHandle) {
            Mage::throwException(
                "Could not open attribute label export file $sFile"
            );
        }
        
        foreach (self::TRANSLATE_LABEL_ATTRIBUTES as $sAttributeCode) {
    
            $oAttribute = Mage::getModel('catalog/resource_eav_attribute')
                ->loadByCode('catalog_product', $sAttributeCode);
            
            if (!$oAttribute->getAttributeId()) {
                echo "WARNING: Cannot load attribute: $sAttributeCode! \n";
        
                return false;
            }
    
            $oAttributeOptionsModel = Mage::getModel(
                'eav/entity_attribute_source_table'
            );
            $oAttributeOptionsModel->setAttribute($oAttribute);
    
            $aOptions = $oAttributeOptionsModel->getAllOptions(false);
            
            foreach (Mage::app()->getStores(true) as $iStoreId => $oStore) {
                foreach ($aOptions as $aOption) {
                    fputcsv(
                        $rFileHandle,
                        [
                            $oAttribute->getAttributeCode(),
                            $oStore->getCode(),
                            $aOption['label']
                        ],
                        self::CATEGORY_PRODUCT_POSITIONS_CSV_DELIMITER,
                        self::CATEGORY_PRODUCT_POSITIONS_CSV_ENCLOSURE
                    );
                }
            }
        }
    }

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.