简体   繁体   中英

Magento Sort Collection By Product Attribute Value Count

I'm currently working on a Magento site which uses layered navigation and a large number of custom attributes for its navigation. What I am looking to do is to add a new "Sort By" option into the listing (selected by default for certain categories) which should help to improve the relevance of the products displayed for a large number of customers.

The site is selling knitting yarn.

Here's how the client would like it to work -

  • We have a custom attribute set up called "colour_name" with values such as red, green, yellow, purple etc.

  • Due to the nature of the product, sometimes this classification can be somewhat ambiguous, so many products are listed under multiple categories, for example, a bluey-green yarn will have both the "blue" and "green" options selected.

  • If a customer selects "blue" from the layered navigation, we would like the products with just the blue option selected to be listed first, followed by products with 2 colours, then 3, and so on.

I don't have a huge amount of experience with collections, however I am familiar with basic sorting and filtering. The collection we need to work with is in app/design/frontend/default/[template_name]/template/catalog/product/list.phtml on the following line:

<?php $_productCollection=$this->getLoadedProductCollection() ?>

I'm not sure whether to perform this sort in the list.phtml file or further down in the helper for categories / sorting.

Unfortunately the site isn't live yet so I can't show you an example, hopefully the description is sufficient but please feel free to ask for clarification on any points if needed.

You may filter and sort collections in Magento via the addAttributeToFilter ( addFieldToFilter for collections other than a productCollection) and addAttributeToSort .

By default, Magento , uses 'ORDER BY' in the underlying SQL for the sort, you should thereby use an approach that uses a custom module with a custom sort on a loaded collection.

There is a very could answer here if you'd like to try that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM