0

Using Magento CE 1.9.0.1.

We're trying to find a way to sort products on our site based on which attribute set they were created with. We sells clothes on our site so we have a number of attribute sets such as hats, pants, tees, accessories, etc..

I have the means to sort by attribute:

$_productCollection = Mage::getModel('catalog/product')->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToSort('display_order', 'ASC');
$_helper = $this->helper('catalog/output'); 

But this covers attributes within an attribute set. Is there a way to include an "attribute set" field into the catalog template for sorting? I'm currently working with app/design/frontend/mypackage/mytheme/template/catalog/product/list.phtml.

Example: We have a "New" page where products from all the various categories will show. You'll see a new hat, new t-shirt, new pants, etc. but we would like all the new hats to be grouped together (which would be done by sorting attribute set), pants to be together, t-shirts, and so.

**EDIT: ** I found a way to access the product object's attribute set... https://stackoverflow.com/questions/2091375/how-do-i-get-attribute-set-name Now I just to know how to sort by that piece of data.

2 Answers 2

2

I had used observer for this in your custommodule and in that etc/config.xml under <global> tag use:

<events>
    <catalog_product_collection_load_after>
        <observers>
            <product_load_before>
                <type>singleton</type>
                <class>Yourpackagename_Yourmodulename_Model_Observer</class>
                <method>load_productsby_attributesetid</method>
            </product_load_before>
        </observers>
    </catalog_product_collection_load_after>
</events>

Also in custommodule/Model/Obeserver.php under your modules Model folder add following code

<?php
    class Yourpackagename_Yourmodulename_Model_Observer{
        public function load_productsby_attributesetid($observer){
            $collection = $observer->getCollection();
            $collection->addAttributeToSelect('*')->addAttributeToSort('attribute_set_id', 'asc');
            return $collection;
        }
    }
?>
8
  • I have created the custom module and swapped the naming with my own. How do I make use of this function? Do I execute it on the catalog template of my choosing?
    – sparecycle
    Feb 4, 2015 at 21:51
  • just check my edited answer Feb 5, 2015 at 4:02
  • I get that part. But I try to call the function load_productsby_attributesetid() from my list.phtml file and it seems to be undefined.
    – sparecycle
    Feb 5, 2015 at 6:40
  • dont call load_productsby_attributesetid() in your list.phtml Feb 5, 2015 at 6:42
  • justr add this above 2 content at to flies that it.dont call method.rest is done Feb 5, 2015 at 6:43
0

You can try sorting by 'attribute_set_id'.

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.