Magento – Get store view attribute value from collection

attributesmagento-1.9product-collection

I'm using the following call to get a product collection:

        $collection = Mage::getModel('catalog/product')
        ->getCollection()
        ->setStoreId($storeId)
        ->addStoreFilter($storeId)
        ->addAttributeToSelect(array('attribute_name1','attribute_name2'))
        ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

The product "attribute_name1" has never been set on the default view of the product in Magento admin, but it has been set on the store view level.

If I call the attribute in my code with $product->getAttributeName1() it returns null – even if a value has been set on the store view level in admin.

If I set the default value of the attribute to "foo" and the store view value af the attribute to "bar" the call $product->getAttributeName1() returns "bar" as expected.

My questions is: How can I structure my collection call to ensure that my $product->getAttributeName1() call returns the store view level value, even if a default value hasn't been set?

Best Answer

You can load each product with $product->load(), but you will kill your website performance.

Use the following raw attribute access code:

$myValue = Mage::getResourceModel('catalog/product')
  ->getAttributeRawValue($productId, 'my_attribute_name', $storeId);

It will be much much faster ;) (of course depending on the number of attribute you need to load )

Related Topic