Magento: How to get attribute values used in products

后端 未结 4 1247
北荒
北荒 2021-01-30 03:43

How I can get attribute values for some attribute that are used at least in one product?

4条回答
  •  礼貌的吻别
    2021-01-30 04:18

    I believe you are not trying to read an attribute value of a product model, but get a list of all used values for a specific attribute.

    "Plain" attributes

    Plain attributes are all attributes that don't use a select or multiselect for input, but a text or textarea field or something similar.

    For these attributes use this:

    // specify the attribute code
    $attributeCode = 'name';
    
    // build and filter the product collection
    $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToFilter($attributeCode, array('notnull' => true))
            ->addAttributeToFilter($attributeCode, array('neq' => ''))
            ->addAttributeToSelect($attributeCode);
    
    // get all distinct attribute values
    $usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
    

    "Option" attributes

    If it is a select or multiselect attribute, you still need to map the option ID's to option values. That is the case if you are getting a list of integers instead of human readable labels (e.g. for the color or manufacturer attribute).

    // specify the select or multiselect attribute code
    $attributeCode = 'color';
    
    // build and filter the product collection
    $products = Mage::getResourceModel('catalog/product_collection')
            ->addAttributeToFilter($attributeCode, array('notnull' => true))
            ->addAttributeToFilter($attributeCode, array('neq' => ''))
            ->addAttributeToSelect($attributeCode);
    
    $usedAttributeValues = array_unique($products->getColumnValues($attributeCode));
    
    // ---- this is the different part compared to the previous example ----
    
    // fetch the attribute model
    $attributeModel = Mage::getSingleton('eav/config')
            ->getAttribute('catalog_product', $attributeCode);
    
    // map the option id's to option labels
    $usedAttributeValues = $attributeModel->getSource()->getOptionText(
        implode(',', $usedAttributeValues)
    );
    
    // $usedAttributeValues now contains an array of used values in human readable format
    

    Direct DB query example

    Depending on where you want to do this, here is an example of fetching the values without using a product collection. It is slightly more efficient.
    Only use the following code in resource models, as thats where DB related code belongs.
    This is meant as an educational example to show how to work with Magento's EAV tables.

    // specify the attribute code
    $attributeCode = 'color';
    
    // get attribute model by attribute code, e.g. 'color'
    $attributeModel = Mage::getSingleton('eav/config')
            ->getAttribute('catalog_product', $attributeCode);
    
    // build select to fetch used attribute value id's
    $select = Mage::getSingleton('core/resource')
            ->getConnection('default_read')->select()
            ->from($attributeModel->getBackend()->getTable(), 'value')
            ->where('attribute_id=?', $attributeModel->getId())
            ->distinct();
    
    // read used values from the db
    $usedAttributeValues = Mage::getSingleton('core/resource')
            ->getConnection('default_read')
            ->fetchCol($select);
    
    // map used id's to the value labels using the source model
    if ($attributeModel->usesSource())
    {
        $usedAttributeValues = $attributeModel->getSource()->getOptionText(
            implode(',', $usedAttributeValues)
        );
    }
    
    // $usedAttributeValues now contains an array of used option value labels
    

提交回复
热议问题