0

Hi i created a multi select product attribute with setup.php and it got created. i used the following code:

 $installer = $this; 
 $installer->startSetup();   
 $installer->addAttribute('catalog_product', 'author_name',array( 
'label' => 'Name of The Author', 
'type' => 'varchar', 
'input' => 'multiselect', 
'backend' => 'eav/entity_attribute_backend_array', 
'frontend' => '', 
'source' => '', 
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, 
'visible' => true, 
'required' => false, 
'user_defined' => false, 
'searchable' => false, 
'filterable' => false, 
'comparable' => false, 
'option' => array ( 'value' => array('optionone' => array('Carolyne Aarsen'), 
                                     'optiontwo' => array('Jennie Lucas'), 
                                     'optionthree' => array('Anna Adams'),
                                     'optionfour' => array('Kathryn Albright'), 
                                     'optionfive' => array('Trisha Alexander'), 
                                     'optionsix' => array('Vincent Alexandria'),
                                     'optionseven' => array('Louise Allen'), 
                                     'optioneight' => array('Abby Green'), 
                                     'optionnine' => array('Laura Abbot'),
                                     'optionten' => array('Caroline Anderson'), 
                                     'optioneleven' => array('Victoria Aldridge'), 
                                     'optiontwelve' => array('Harper Allen'),
                                     ) 
                  ), 
'visible_on_front' => false, 
'visible_in_advanced_search' => false, 
'unique' => false, 
'group' => 'General'
)); 
$installer->endSetup();

Then i want to get the author_name attribute value for each product in every category that is included in naigation menu of the site i wrote the following query to get the attribute value:

    $categories = Mage::getModel('catalog/category')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToFilter('include_in_menu', '1');
    foreach ($categories as $_category):
    if ($_category->getName() != 'Root Catalog'): 
     $category = Mage::getModel('catalog/category')->load($_category->getId());
     $products = $category->getProductCollection()
                       ->addCategoryFilter($category)
                       ->addAttributeToSelect('author_name')
                       ->setPageSize(9); 
         $author_names = array(); 
         foreach ($products as $_product):                             
        $author_names[] = $_product->getAttributeText('author_name');
         endforeach;
         print_r($author_names); 
    endif;
    endforeach; 

the array is not printing anything. when i debug this i am not getting author_name attribute in the collection.

is there any thing wrong in my query. please help me out

4

2 回答 2

0

Try following code to display custom attributes

<?php echo $_product->getResource()->getAttribute('author_name')->getFrontend()->getValue($_product); ?>
于 2012-12-04T05:38:43.837 回答
0

Hello check following query may be help you

get product id & using this id get product auther name

$reqProdid = Mage::registry('current_product')->getId();
$selected_auther =Mage::getModel('catalog/product')
    ->load($reqProdid)
    ->getData('author_name');
于 2012-12-04T05:45:03.697 回答