Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 5
Problem with attributes
 
spider
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2007-10-12
 

Hi.

I have a big problem with attributes class engine. I add new attribute called “promotion” and put this attribute into “Default” set. I was added new product with this attribute and here start my problem. I want to display value of this attribute in the list of products. How can I do this ?

I edit this file: app/core/Mage/Catalog/Product.php

and in that file I added new method:

public function Promotion(){
    
        $attribute 
$this->getResource()->getAttribute('promotion');
        return 
$attribute ;

    
}

this method I call in file witch show me the list of products: app/design/frontend/default/default/template/catalog/product/list.phtml
I added this piece of code:

<?=$_product->Promotion()?>

but I don’t see my value of this attribute but this line of text: “[535,10,,promotion,eav/entity_attribute,eav/entity_attribute_backend_default,text,,,text,promotion,,,0,1,0,1,,1,0,1,1,0,0,0]”

What I have to do for show value of this attribute ?
Help and thanks.

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

@spider: hi, if your attribute is text type, you can just do

<?=$_product->getPromotion()?>

If it is a dropdown, in current release it’s like this:

<?=$_product->getResource()->getAttribute('promotion')->getSource()->getOptionText($_product->getPromotion())?>

In next release it will be:

<?=$_product->getAttributeText('promotion')?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
spider
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2007-10-12
 

I have text value in database for attribute promotion but this line of code

<?=$_product->getPromotion() ?>

unfortunetly doesn’t work :/ It shown me nothing.
Is there some different ways to do that ?
This is very important to me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sponsorewiczowiczew
Jr. Member
 
Avatar
Total Posts:  5
Joined:  2007-09-06
 

I have this problem too. I var_dump $_data - Varien_Object field and there is no my attribute. Magento doesn`t select it from db when it create Mage_Catalog_Model_Product object.

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

Which page and template are we talking about here?

 
Magento Community Magento Community
Magento Community
Magento Community
 
spider
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2007-10-12
 

We talking about page witch list products in category

this class is responsible(I supose) for engine of this view: app/code/core/Mage/Catalog/Model/Product.php
and template file is: app/design/frontend/default/default/template/catalog/product/list.phtml

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

@spider: if you want to add custom attributes to product listing, you will have to add them in the layout xml.

To save time we do not load all the available attributes for all product in the list, only necessary ones (name, image, url_key, etc.)

Edit main.xml, find handles <catalog_category_default> and <catalog_category_layered> and replace in both:

<block type="catalog/product_list" name="product_list"></block>
with:
<block type="catalog/product_list" name="product_list">
   <
action method="addAttribute"><code>promotion</code></action>
</
block>

You will need to repeat <action> tag for each additional attribute you wish to see in product list.

 
Magento Community Magento Community
Magento Community
Magento Community
 
spider
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2007-10-12
 

Now it works fine.
Very helpful.
Thank You.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mateusz Papiernik
Member
 
Total Posts:  33
Joined:  2008-01-04
 
Moshe - 22 November 2007 12:17 AM

@spider: if you want to add custom attributes to product listing, you will have to add them in the layout xml.

To save time we do not load all the available attributes for all product in the list, only necessary ones (name, image, url_key, etc.)

Edit main.xml, find handles <catalog_category_default> and <catalog_category_layered> and replace in both:

<block type="catalog/product_list" name="product_list"></block>
with:
<block type="catalog/product_list" name="product_list">
   <
action method="addAttribute"><code>promotion</code></action>
</
block>

You will need to repeat <action> tag for each additional attribute you wish to see in product list.

Moshe, can I do the same when using {{block}} tag in my content pages? I’m displaying products attached to root catalog category on homepage (as they are not visible anywhere else, and therefore can be easily used for selected products) using:

{{block type="catalog/product_list" show_root_category="true" template="catalog/product/list_home.phtml"}}

I have created additional template list_home.phtml to accomplish my design. What I need is to access thumbnail image in the template. For other category listings I have added:

<block type="catalog/product_list" name="product_list">
    <
action method="addAttribute"><code>thumbnail</code></action>
</
block>

to catalog.xml in proper places. But how can I do the same for {{block}} tag? I tried using layout update for content page with

<reference name="content">
<
block type="catalog/product_list" name="product_list" show_root_category="true" template="catalog/product/list_home.phtml">
    <
action method="addAttribute"><code>thumbnail</code></action>
</
block>
</
reference>

But it doesn’t work. Magento shows exception ‘Mage_Core_Exception’ with message ‘Can’t retrieve current category object’ in D:\Praca\www\magento\app\Mage.php:371

Which is theoretically obvious, because none is set - but show_root_category="true" avoids the message in {{Block}} but doesn’t in layout update.

What can I do to access thumbnail image within my template on homepage (CMS page)? I have managed to do it with hacking a core, by adding

/* ADD - REMOVE WHEN MOSHE REPLIES */
        $this->_getProductCollection()->addAttributeToSelect('thumbnail');
        
/* ADD - REMOVE WHEN MOSHE REPLIES */

to _beforeToHtml() in core/Mage/Catalog/Block/Product/List.php - but I don’t find the idea clean. I would love to replace it with something not intruding the core.

Thanks in advance,
Mateusz

 
Magento Community Magento Community
Magento Community
Magento Community
 
patriveri
Jr. Member
 
Total Posts:  30
Joined:  2008-02-23
 

Mateusz,

I also need to display custom attributes from a cms page. Did you figure out a way of doing this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spek
Sr. Member
 
Total Posts:  77
Joined:  2007-11-28
 

The above solutions didn’t work for me either, /I’m unable to get the manufacturer for the product. I’m trying to list products with “Manufacturer Productname”.

 
Magento Community Magento Community
Magento Community
Magento Community
 
daFunk
Jr. Member
 
Total Posts:  9
Joined:  2008-04-08
 

Hi,

this is also very important to me and i tried to reproduce it like Moshe posted:

In Mage/Catalog/Block/Product.php

public function getAttributeText()
    
{
        
return $this->getAttributeText('myAttributeName');
    
}

and replaced in layout/catalog.xml to:

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
           <
action method="addAttribute"><code>myAttributeName</code></action>
    </
block>
and

<block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
          <
action method="addAttribute"><code>myAttributeName</code></action>
</
block>
</block>

but it wont work. Shows nothing!
pls. help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
Spek
Sr. Member
 
Total Posts:  77
Joined:  2007-11-28
 

In the following bit of code you easily make the mistake of putting the addAttribute action after the autoclosing <block> with the list.phtml template that preceeds it.
In the original code remove the “/” before the “>” and close the block manually after adding the attribute.
This is what it looks like:

<block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
   <
block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
      <
action method="addAttribute"><code>manufacturer</code></action>
   </
block>
</
block>

I then show the manufacturer in list.phtml like this:

<?=$_product->getAttributeText('manufacturer')?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
daFunk
Jr. Member
 
Total Posts:  9
Joined:  2008-04-08
 

thanks Ben* for your help but it doesnt work.

Im confused about the Product.php. Witch is the right one to do the modification?
Model/Product.php or Block/Product.php?

This is what happens:
Fatal error: Call to a member function getSource() on a non-object in /kunden/205762_85051/webseiten/site/app/code/core/Mage/Catalog/Model/Product.php on line 703

thanks and greets from germany

 
Magento Community Magento Community
Magento Community
Magento Community
 
capple_
Jr. Member
 
Total Posts:  17
Joined:  2008-04-02
 

Hi,

I made the same changes as Ben’s post described and it doesn’t work. I’m using Magento v1.0.

What are the files that I need to modify and what are the exact modifications needed? Can someone provide precise instructions?

Here’s what I did:

1) app / design / frontend / default / default / layout / catalog.xml
In both <catalog_category_default> and <catalog_catagory_layered>
Replace the code within <reference name="content"> and </reference>
With:

<block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
    <
block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
        <
action method="addAttribute"><code>part_no</code></action>
    </
block>
</
block>

2) app / design / frontend / default /default / template / catalog / product / list.phtml
Add:

<?='Part No.: '.$_product->getAttributeText('part_no')?>

But I can’t get the custom attribute to show.

Am I missing a modification in another file?

Please advise!

 
Magento Community Magento Community
Magento Community
Magento Community
 
tobillo
Member
 
Avatar
Total Posts:  46
Joined:  2008-04-07
Germany
 

Hi… i have exactly the same Problem like the user capple_

..... need help!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 5