Question

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.

Was it helpful?

Solution

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;
        }
    }
?>

OTHER TIPS

You can try sorting by 'attribute_set_id'.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top