0

I've been trying to create a solution to this for some time now, and have been to the ends of the internet and back, maybe I missed something.

What I am trying to do is sort the product collection in the category list or grid view by product view count (sort by Popularity). I've looked into Mage_Reports_Model_Resource_Report_Collection but I don't think this will achieve what I am trying to do here. Is there a filterable product attribute that I can create that will allow this type of sorting? Or should I look at overriding setCollection() in Mage_Catalog_Block_Product_List_Toolbar? Or maybe there's another way to do this that I haven't thought of yet.

As far as I understand getAvailableOrders() will get a list of allowed sort orders based on what is set in the Admin panel, but I can't quite figure it out from there.

2

If you're just saving the attribute, you should try something like this:

$product->setData('product_view_count', $value)
  ->getResource()->saveAttribute($product, 'product_view_count');

Which will simply save that attribute to the product, rather than attempt to save the entire product.

2
  • Ah very nice, didn't know you can save individual attributes without saving the entity as a whole.
    – DWils
    Apr 21 '14 at 21:41
  • Where To set This Code.
    – coder
    Sep 14 '15 at 6:16
0

For those interested, what I ultimately ended up doing was creating a new attribute: product_view_count and set the Admin name as Popularity, assigned the attribute to the appropriate sets, then in the product view template I used this code:

$newViewCount = $_product->getData('product_view_count') + 1; 
$_product->setData('product_view_count', $newViewCount); 
$_product->save();

Now that this is in place, all I need to do is configure the attribute as an Integer, and allow it to be used in Sorting. Now I can sort products in the category list view by view count. As an added bonus, setting this attribute as a text field allows me to edit the value directly in the product editor in admin. From here it's pretty easy to figure out how to determine in the list view which product has the highest value and show an image for the most popular item in a specific category.

Edit: Seems that products that don't have a value after doing this throw an integrity constraint SQL exception when saving the product. Trying to figure out why this is happening and will post here when I solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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