0

I am struggling to understand why the msrp attribute is unavailable in ~/template/catalog/product/price.phtml

I can access it fine from within product/view.phtml

I have defined a custom attribute for a product and have no problem accessing it with (for example):

$_product->getAttributeText('custom_attribute');

However:

$_product->getAttributeText('msrp'); 

and

 $_product->getMsrp(); 

both return null.

I would really appreciate figuring out how to

a) access this value but more importantly

b) know why some attributes seem to be available via standard getters, while others aren't

EDIT: So attribute is actually available in price.phtml when viewing single product pages, however in this case i'm calling a category list view.

1
  • okay so, i can access that value via $prod=Mage::getSingleton('catalog/product')->load($_product->getId()); error_log($prod->getMsrp()); this seems incredibly inefficient though to reload the entire product object, what is the better way to do this?
    – baku
    Commented Aug 1, 2015 at 23:40

3 Answers 3

2

Product collections by default load a subset of the products' attributes, unlike $product->load() which loads all product attributes. You have to make sure that the msrp attribute is loaded by the product collection you're using. See Mage/Catalog/etc/config.xml, the config/frontend/product/collection/attributes node:

In your module's config.xml you can add msrp:

<config>
...
    <frontend>   
        <product>
            <collection>
                <attributes>
                    <msrp />
                </attributes>
            </collection>
        </product>
    </frontend>
...
<config>

Alternatively, you can tap into the block that loads the product collection and do a $productCollection->addAttributeToSelect('msrp');. Another option is to also add an observer to the product collection load before event and again add the attribute to the select.

/Edit: If System -> Configuration -> Catalog -> Frontend -> Use Flat Catalog Product is enabled, product collections on the frontend read from the flat tables. That means that the attribute has to be one of the attributes indexed this way otherwise it will be silently ignored (addAttributeToSelect does nothing). An attribute to be indexed in the flat tables has to meet a few conditions that I don't remember exactly right now. I think the attribute has to be Used in product listing, Use in Layered Navigation and something else.

Cheers

/Edit: adding information from the comments

getAttributeText() should be used only for Dropdown and Multiple Select attributes. I'm pretty sure MSRP is a price type attribute. getAttributeText() eventually calls getOptionText() which returns false for regular attributes.

See Mage_Catalog_Model_Product::getAttributeText() and Mage_Eav_Model_Entity_Attribute_Source_Abstract::getOptionText()

2
  • Thank you Adrian, that makes sense. I guess this is basic MVC stuff :) However, do you know why my custom attributes do not follow the same rules/conditions? I can access them via getData() or getAttributeText() without registering them to the template via config.xml
    – baku
    Commented Aug 2, 2015 at 17:15
  • All attributes follow the same conditions, unless there's some custom magic involved :) BTW, getAttributeText() should be used only for Dropdown and Multiple Select attributes. I'm pretty sure MSRP is a price type attribute. getAttributeText() eventually calls getOptionText() which returns false for regular attributes. See Mage_Catalog_Model_Product::getAttributeText() and Mage_Eav_Model_Entity_Attribute_Source_Abstract::getOptionText()
    – Adi
    Commented Aug 2, 2015 at 17:21
0

It is available. I'm able to get the msrp value within price.phtml using either of these :

<?php echo $_product->getData('msrp');?>
<?php echo $_product->getMsrp();?>

Not sure why that wouldn't work for you, are you sure your products have Manufacturer's Suggested Retail Price defined?

There are some good opinions here on magic getters vs getData :

1
  • it looks like it is indeed available in single product (thank you for making me check again), but not in category lists of products.
    – baku
    Commented Aug 2, 2015 at 16:24
0

In order for the attribute to be used on a category page, the Used in Product Listing field for that attribute must be set to "Yes". Additionally, if you have set the Use Flat Catalog Category field to "Yes" under System > Configuration > Catalog > Frontend, you will need to rebuild the Product Flat Data index under System > Index Management before it can be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.