Magento Forum

   
Loading Attributes
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

I can’t seem to find out how to load the attributes of a product on the product view page.  I’ve been trying for a few days and scanning all the forum posts that I could.

This is a grouped product that I’m viewing, and there is a special template Block for its linked sub-products:  app/design/frontend/default/default/template/catalog/product/view/super/group.phtml :31

.
   
31     <?if ($this->getItems()->getSize()):?>
   32     <?foreach 
($this->getItems() as $_item):?>
   33         
<tr>
   
34             <td><?=$_item->getName()?></td>
   
35             <td><?=$_item->getSku()?></td>

The problem I’m having is that each $_item does not have it’s individual attributes loaded, only a core set of attributes like price and name.  The $_items are regular Mage_Catalog_Model_Product objects from what I can tell, just like the super grouped object of the page that I’m viewing.

The block gets its items from Mage_Catalog_Model_Product::getSuperGroupProductsLoaded(), which returns an array of Mage_Catalog_Model_Entity_Product_Link_Collection type objects.

That’s where I get lost.  I’m not sure why there’s only a limited set of attributes loaded for the linked products underneath a super grouped product.  Can anyone show me a way to make sure all the attributes are loaded for an object?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

For some reason, when Mage_Catalog_Model_Products get loaded as in an Entity_Product_Link_Collection, they do not possess their own attributes, and their product_id field is the product_id of their associated product.

This is probably horribly inefficient, but after 3 days of tracing through this code, this is all I could come up with to load attributes.

21     <?
   22 $data 
= array();
   
23 foreach ($this->getItems() as $_item{
   24     $newProduct 
= new Mage_Catalog_Model_Product();
   
25     $newProduct->load($_item->getLinkedProductId());
   
26
   27         $attributes 
$newProduct->getAttributes();
   
28         foreach ($attributes as $attribute{
   29             
if ($attribute->getIsVisibleOnFront() && $attribute->getIsUserDefined()) {
   30                 $value 
$attribute->getFrontend()->getValue($newProduct);
   
31                 if (strlen($value)) {
   32                     $data[$attribute
->getAttributeCode()= array(
   
33                        'label' => __($attribute->getFrontend()->getLabel()),
   
34                        'value' => $attribute->getFrontend()->getValue($newProduct)
   
35                     );
   
36                 }
   37             }
   38         }
   39 ?>

There must be a better way to load attributes of linked products....  why aren’t they loaded when the product is loaded?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

When you instantiate collection, you need to specify which attributes to load.
For example this statement will load all available attributes for products in collection:

$collection Mage::getResourceModel('catalog/product_collection')
//...
    
->addAttributeToSelect('*')
    ->
load();

If you want to have custom logic for selecting which attributes to load, do a loop with

$collection->addAttributeToSelect($attributeCode);

Hope this helps smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 
Moshe - 16 November 2007 10:37 AM

When you instantiate collection, you need to specify which attributes to load.
For example this statement will load all available attributes for products in collection:

$collection Mage::getResourceModel('catalog/product_collection')
//...
    
->addAttributeToSelect('*')
    ->
load();

If you want to have custom logic for selecting which attributes to load, do a loop with
$collection->addAttributeToSelect($attributeCode);

Hope this helps smile

Ooh, that does help a lot!  Thanks for that little pointer. I think I have a new question and that is, “Will this break anything else in the system by loading all attributes for linked products on the group product view page?”

I see this code in Catalog/Model/Entity/Product/Link/Collection.php

174     protected function _loadLinkAttributes($type=null)
  
175     {
  176         
if(!$this->_linkAttributeCollectionLoaded{
  177             $this
->getLinkAttributeCollection()->addLinkTypeData();
  
178
  179             
if(!is_null($type)) {
  180                 $this
->getLinkAttributeCollection()->addFieldToFilter('link_type'$type);
  
181             }
  182
  183             $this
->getLinkAttributeCollection()->load();
  
184             $this->_linkAttributeCollectionLoaded true;
  
185         }
  186
  187         
return $this;
  
188     }

Could I add the call “ ->addAttributeToSelect(’*’)” right after line 180?
What defines a “link_type” attribute in the backend?

I’m trying to show a table on the group product page, an example of this with the default magento product data is the “Magento Red Furniture Set” (/catalog/product/view/id/54/s/magento-red-furniture-set/category/22/).

The affect that I’m going for would be adding to the existing table, which looks like this:

|  Product Name     SKU      Price         Qty
Couch              xxx      | $ 599.99     |  __
Ottoman            xxx      | $ 299.99     |  __
Chair              xxx      | $ 129.99     |  __

into something like this:

|attr 1  |attr 2  |  Product Name     SKU      Price         Qty
|value 1 |value 2 Couch              xxx      | $ 599.99     |  __
|value 1 |value 2 Ottoman            xxx      | $ 299.99     |  __
|value 1 |value 2 Chair              xxx      | $ 129.99      |  __

... where “attr1” and “attr2” come from a third, non-frontend attribute “table-attribs”.  So, for the couch example, I can define “table-attribs = ‘finish, model, countryoforigin’;” in the backend and apply it only to the grouped product couch… Does that make sense?  I think using the attribute system this way is probably the best way to customize Magento so that non-programmers still have some back-end screens to make adjustments, but it’s not hard coding too much into the base of Magento.

After I get all this I think I’m going to adapt a custom Block for Catalog/Blocks/View/Super/Group and the template, and hopefully use the config.xml to properly switch out the code as you explained in a different post.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

try this: override Mage_Catalog_Block_Product_View_Super_Group::getItems() with

public function getItems()
{
   $collection 
Mage::registry('product')->getSuperGroupProducts();
   if (!
$collection->getIsLoaded()) {
      $collection
->addAttributeToSelect('*')->load();
   
}
   
return $collection;
}
There are many ways to configure the resulting output so for now i’ll leave that to you smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

Wow, perfect.  Thanks so much.  I was trying to put in that function too deep inside the libraries.  Makes much more sense up at the Block layer.  cool grin

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top