开发者

Cannot get new product attribute in grid display

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜