0

I've created a module that is intended to allow me to add custom sort features to my catalog pages (my product collection). For whatever reason, it returns my collection no problem but fails to honor any form of sort that I apply to it. Here is my Observer.php code

//File: app/code/local/MyExtensions/Attsetfilter/Model/Observer.php

<?php
class MyExtensions_Attsetfilter_Model_Observer
{
    public function load_productsby_attributesetid($observer) {
        $collection = $observer->getCollection();
        $collection->addAttributeToSort('attribute_set_id', 'asc');
        echo "Extension is being called";
        return $collection;
    }
}

I know my product collection is being passed through here because if I comment out the first line of my public function ("$collction = ...") it returns nothing. I've attempted to add filters to the function like so

        $collection->addAttributeToSort('attribute_set_id', 'asc');

But it never seems to honor them. I've seen other methods of filtering a product collection such as:

$collection = $this->getCollection()
        ->setOrder('fieldname', 'asc')
        ->setOrder('fieldname2');

But that snippet won't work in my extension code either. Is there something obvious that I am doing wrong? Thanks in advance.

EDIT: Here is my modules config.xml file as well:

<?xml version="1.0"?>
<config>
  <modules>
    <MyExtensions_Attsetfilter>
      <version>0.1.0</version>
    </MyExtensions_Attsetfilter>
  </modules>
  <global>
    <events>
    <catalog_product_collection_load_after>
      <observers>
        <product_load_before>
          <type>singleton</type>
          <class>MyExtensions_Attsetfilter_Model_Observer</class>
          <method>load_productsby_attributesetid</method>
        </product_load_before>
      </observers>
    </catalog_product_collection_load_after>
    </events>
    <helpers>
      <attsetfilter>
        <class>MyExtensions_Attsetfilter_Helper</class>
      </attsetfilter>
    </helpers>
  </global>
</config>

3 Answers 3

3

Is there something obvious that I am doing wrong?

Yes, you observe catalog_product_collection_load_after instead of catalog_product_collection_load_before. At this point the collection is already loaded from the database.

2
  • Switching it to "before" instead of "after" seems to stop it from functioning all together.
    – sparecycle
    Feb 12, 2015 at 14:39
  • what do you mean? Feb 12, 2015 at 16:13
0

Try using getSelect()->order() like this:

$collection->getSelect()->order('attribute_set_id asc');
5
  • I'm afraid that doesn't have any affect on sorting. Is it by chance supposed to be setOrder?
    – sparecycle
    Feb 11, 2015 at 20:18
  • No, it's order(). I've had it work for me in the past when other methods failed. Feb 11, 2015 at 20:20
  • Gotcha. Thanks anyways. Maybe there is something I'm overlooking.
    – sparecycle
    Feb 11, 2015 at 20:22
  • Is it not honoring ANY field that you try sort by or just that one? Maybe attribute_set_id isn't a loaded attribute for the collection. You could try $collection->addAttributeToSelect('*') prior to your sort statement to see if that helps. Feb 11, 2015 at 20:42
  • Not sorting for anything. Let me try that...
    – sparecycle
    Feb 11, 2015 at 20:43
0

I faced the same problem.Make sure order_no is stored as Integer in database.That was the mistake I made. Try this one.

$collections=$this->getCollection()->setOrder('order_no','ASC');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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