Hello there. Welcome back to Magento blog!

As you may know, there are two types of storing a product when loading collection in Magento: EAV and Flat. The Flat has the higher performance than EAV while EAV is more dynamical than Flat. My tutorial today will talk about flat catalog product and focus on 3 main following parts:

  • Enable flat catalog product
  • Add a custom field to flat table
  • Save product data to flat table

1. Enable flat catalog product

To use flat catalog product, first you need to enable it. You can login to the admin page and navigate to System -> Configuration then choose tab Catalog -> Catalog. In the Frontend field set, choose Yes for the field Use Flat Catalog Product.

2. Add a custom field to flat table

Flat database is used to improve the loading speed of a product collection. However, it’s not that all fields of a product are stored in a flat table. Thus you should know how to add a custom attribute to the product stored in the flat table.

• Step 1: Add your custom attribute to the list of product’s system attributes through config.xml file:

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

• Step 2: Add your custom attribute satisfying one of the followings:

  • backend_type = static
  • used_in_product_listing = 1
  • used_for_sort_by = 1
  • is_filterable > 0 (in configure, global/catalog/product/flat
  • add_filterable_attributes must be enabled)

• Step 3: Use event catalog_product_flat_prepare_columns to add columns to the flat table:

Mage::dispatchEvent('catalog_product_flat_prepare_columns', array(
'columns' => $columnsObject
));

3. Save product data to flat table

Flat product data is stored by indexer model. This model runs when you reindex flat data for the product. To reindex flat data, you can login to the backend, go to System -> Index management and click on the link Reindex Data in the Product Flat Data row. Please take a look at the picture below for more details:

Besides being stored when reindexing, flat data is stored when there’s any change in product data by the function afterCommitCallback. This function is called from catalog/product model and it will call an event to process index data for flat table.

Nice code 🙂 Just go to our Magento blog to get other helpful tutorials on Magento developer exam.

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment