2

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?

1

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 )

  • Thank you for your answer! I was wondering if there was a way to add the values to the initial collections call, to avoid the additional db queries with the getAttributeRawValue() – dannevang Feb 18 '16 at 10:48
  • addAttributeToSelect should work, try debugging your query with getSelect to understand why it is not adding your two attributes. – Phoenix128_RiccardoT Feb 18 '16 at 10:51
  • Also try to call "$collection->load()", I'm not sure that additional attributes are loaded within an iterator. – Phoenix128_RiccardoT Feb 18 '16 at 10:54
  • They are added as expected. The issue is that they are only added if a default value is made on the product attribute. If the default value isn't set, but a store view value is, the value is not added to the collection. – dannevang Feb 18 '16 at 10:59
  • A default value should always exists. Let me guess... did you use Magmi as product importer? – Phoenix128_RiccardoT Feb 18 '16 at 11:00
0

Try this code

<?php 
$storeID = 3;  // Your store id here.
$store = Mage::app()->getStore($storeID);

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

foreach($collection as $product) {
    echo $product->getSku().'=>'.$product->getAttributeName1();
    echo '<br>';
    echo $product->getSku().'=>'.$product->getAttributeName2();
} 
?>
0

If you want to fetch the product collection with few attribute, for a specific store view scope, then this code snippet is helpful for you.

$storeId=1; //YOUR STORE VIEW ID.
Mage::app()->setCurrentStore($storeId);
$collection = Mage::getModel('catalog/product')->getCollection()
              ->addAttributeToFilter('type_id', 'simple')
              ->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
              ->addAttributeToSelect(array('name','ATTRIBUTE_NAME1','ATTRIBUTE_NAME2'));
foreach($collection as $product){
    echo "Product Name: ".$product->getName();
    echo "Product Name: ".$product->getATTRIBUTE_NAME1();
}

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.