Magento Product Attribute Get Value

后端 未结 13 1596
暗喜
暗喜 2020-12-02 05:32

How to get specific product attribute value if i know product ID without loading whole product?

相关标签:
13条回答
  • 2020-12-02 06:02

    You don't have to load the whole product. Magentos collections are very powerful and smart.

    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection->addAttributeToFilter('entity_id', $product->getId());
    $collection->addAttributeToSelect('manufacturer');
    $product = $collection->getFirstItem();
    $manufacturer = $product->getAttributeText('manufacturer');
    

    At the moment you call getFirstItem() the query will be executed and the result product is very minimal:

    [status] => 1
    [entity_id] => 38901
    [type_id] => configurable
    [attribute_set_id] => 9
    [manufacturer] => 492
    [manufacturer_value] => JETTE
    [is_salable] => 1
    [stock_item (Varien_Object)] => Array
        (
            [is_in_stock] => 1
        )
    
    0 讨论(0)
  • 2020-12-02 06:04

    You could write a method that would do it directly via sql I suppose.

    Would look something like this:

    Variables:

    $store_id = 1;
    $product_id = 1234;
    $attribute_code = 'manufacturer';
    

    Query:

    SELECT value FROM eav_attribute_option_value WHERE option_id IN (
        SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET(
            option_id, 
            (SELECT value FROM catalog_product_entity_varchar WHERE
                entity_id = '$product_id' AND 
                attribute_id = (SELECT attribute_id FROM eav_attribute WHERE
                    attribute_code='$attribute_code')
            )
        ) > 0) AND
        store_id='$store_id';
    

    You would have to get the value from the correct table based on the attribute's backend_type (field in eav_attribute) though so it takes at least 1 additional query.

    0 讨论(0)
  • 2020-12-02 06:06

    If you have an text/textarea attribute named my_attr you can get it by: product->getMyAttr();

    0 讨论(0)
  • 2020-12-02 06:11

    You can get attribute value by following way

    $model = Mage::getResourceModel('catalog/product');
    $attribute_value = $model->getAttributeRawValue($productId, 'attribute_code', $storeId);
    
    0 讨论(0)
  • 2020-12-02 06:12

    It seems impossible to get value without loading product model. If you take a look at file app/code/core/Mage/Eav/Model/Entity/Attribute/Frontend/Abstract.php you'll see the method

    public function getValue(Varien_Object $object)
    {
        $value = $object->getData($this->getAttribute()->getAttributeCode());
        if (in_array($this->getConfigField('input'), array('select','boolean'))) {
            $valueOption = $this->getOption($value);
            if (!$valueOption) {
                $opt = new Mage_Eav_Model_Entity_Attribute_Source_Boolean();
                if ($options = $opt->getAllOptions()) {
                    foreach ($options as $option) {
                        if ($option['value'] == $value) {
                            $valueOption = $option['label'];
                        }
                    }
                }
            }
            $value = $valueOption;
        }
        elseif ($this->getConfigField('input')=='multiselect') {
            $value = $this->getOption($value);
            if (is_array($value)) {
                $value = implode(', ', $value);
            }
        }
        return $value;
    }
    

    As you can see this method requires loaded object to get data from it (3rd line).

    0 讨论(0)
  • 2020-12-02 06:12

    First we must ensure that the desired attribute is loaded, and then output it. Use this:

    $product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>'));
    $attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product);
    
    0 讨论(0)
提交回复
热议问题