How to Sort Associated Products in Grouped Product in Magento

grouped-productsmagento-1.9

I have an issue which I'm not able to find a solution here and is how to sort all the associated products of a grouped product automatically for every grouped product in the store?

I have 1850 grouped products with their associates but I want to sort these associated products by price from the cheapest to the most expensive. (Up to down)

Anyone know how to do this?

Best Answer

There are two ways to achieve this.

  1. Rewrite the method responsible for creating grouped product association. This saves the positioning in the DB.

For #1, you will need to look at Mage_Catalog_Model_Resource_Product_Link::saveProductLinks. Essentially, in the foreach ($attributes as $attributeInfo) loop of this method, one of the iterations will be for the catalog_product_link_attribute_int table, which hold the positions under the column value when product_link_attribute_id = 2.

You'll need to somehow retrieve the product prices and use that as the sorting index. I recommend pre-loading them into an easily accessible array, so that all you need to do is load it once and access it during the entire save.

  1. Sort on-the-fly when the products get rendered on the frontend. This does not save the positions.

This is easier but slower and only superficial. You'll just need to find the block responsible for rendering the group products and sort the collection or array of products.

I recommend #1 and it's pretty straightforward once you understand the method saveProductLinks.

Related Topic