Magento – Additional Properties in the Product Listing

MagentoThe Magento E-commerce platform is a highly customizable online store, with the full source code provided for advanced manipulation and coding.  With the flexibility, however, often comes the challenge of performance optimization.  For example, the default product listing screens only select a subset of the fields from the database that are directly necessary to render the listing.  In the event that the product listing needs to be customized to pull additional information from the database, the underlying PHP code needs to be updated as well.

The primary Magento product listing template is stored under “app/design/frontend/base/default/template/catalog/product/list.phtml”.  This can be customized for a particular template by copying it to the folder “app/design/frontend/default/TEMPLATE/template/catalog/product/list.phtml”, replacing the uppercase word “TEMPLATE” with the name of the custom template.  After copying the file, the content can then be changed under the particular template folder, preventing loss of changes during store version upgrades.

The actual selection of products is encapsulated by the following function at the top of the list.phtml file:

$_productCollection= $this->getLoadedProductCollection();

In this example, we will add one additional property to the select query, in order to add the “custom_design_to” date to the listing:

$_productCollection = clone $this->getLoadedProductCollection();

The actual property name can be found by running a temporary debug dump of all the properties, and then searching for the name of the property in the print_r dump:

$_productCollection = clone $this->getLoadedProductCollection();
foreach($_productCollection as $_product){

Note that putting an asterisk in the “addAttributeToSelect” function will return all properties, however this will also significantly increase overhead in the query.  While this may suffice for bringing all properties from the database, it is recommended to locate the exact names of the additional properties and add only those to the query.

Finally, with the information returned from the database, the last step is to display it in the listing.  In the body of the product loop, use the get accessor to display the property to the screen:

echo htmlspecialchars($_product->getcustom_design_to());

Append the property name to the word “get”, and execute it as a class function on the _product variable.  Depending on how the property will be used, it will likely be necessary to escape HTML characters using the htmlspecialchars function to help prevent display errors or script injection attacks.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.

RSS Twitter LinkedIn Facebook Email

Leave a Reply

Your email address will not be published. Required fields are marked *