views:

467

answers:

1

I added a new attribute to my products(a boolean "yes/no" field). It is a variable to enable/disable the price from displaying on the product detail page, and grid view.

I managed to get it work on the product info page. But on product grid page I cant seem to access that variable. Specifically, the template i am working with is catalog/product/price.phtml. From what i can tell, the price is being displayed by the same group of if-statements on both the product detail page, and grid page. This has me confused because i cant find any code on that template to handle multiple products, just a bunch of nested if statements.

this is how im attempting to access this new variable using $_displayPrice. on line 36 of catalog/product/price.html

<?php $_product = $this->getProduct();  ?>

<?php $_id = $_product->getId() ?>
<?php $_displayPrice = $_product->getDisplayPrice() ? "Yes" : "No";

echo $_displayPrice;?>

What has me further confused is that when display $_product->getData(), my new variable isn't anywhere among that data.

thanks in advance

+1  A: 

Okay, first the reason your attributes won't load. Magento uses an EAV model to store catalog data. That means that (mostly) every piece of data in the getData array is coming from a table join. That means that Magento is stingy about how it requests data from the database. If it doesn't need your field, it won't bother to include it (and will save some query time).

So, when loading a catalog category page, Magento uses Mage_Catalog_CategoryController and the view action. Through the magic of Magento, this subsequently calls Mage_Catalog_Model_Layer::getProductCollection, which loads all the necessary attributes. How does it choose which attributes to include, you say? It gets them from the configuration in Mage_Catalog_Model_Config::getProductCollectionAttributes of course! So, if we want to include your new attribute, we'll need to change the configuration.

Looking at the config file, the path that handles which columns are loaded is frontend/product/collection/attributes, so you'll need to add the following to an extension's config.xml file:

<frontend>
    <product>
        <collection>
            <attributes>
               <display_price /> 
            </attributes>
        </collection>
    </product>
</frontend>

That will add the display_price attribute to the list of attributes to load into the product collection by default, and you should now be able to call $_product->getDisplayPrice() and get the data you're looking for.

Make sure to clear your cache before you expect to see any changes!

Hope that helps.

Thanks, Joe

Joseph Mastey
That demystifies EVERYTHING! Worked around the issue by adding my if-statement to catalog/product/view.html instead, where it only affects the product detail. Ideally i can use the info you gave me to fix price.phtml so i dont add soo many unessasary if-statements in random templates.Thanks yo!
russjman
I still cant seem to access the new attributes. I added them to the list in app/core/Mage/Catalog/etc/config.xml, and i've diabled the site cache. I assume that adding the there would make them accessable in template/catalog/product/list.phtml.
russjman