views:

134

answers:

1

Hello all,

I am currently trying to make a special sort function for a Magento category page.

I have several attributes, which I need to use for sorting:

The first attribute is named designers. This attribute is set on a configurable product.

The next attributes are named colour and size. These are not set on the configurable product itself, but on the ”simple products”, that I combine to make the configurable product.

$attributes_designers = $this->getRequest()->getParam('designers');   
$attributes_colors = $this->getRequest()->getParam('color');
$attributes_sizes = $this->getRequest()->getParam('size');

$currentCategory =  Mage::getModel('catalog/layer')->getCurrentCategory(); 
$_productCollection = $currentCategory->getProductCollection();

if(count($attributes_designers)>0 and !in_array("ALL",$attributes_designers)) {        
    $_productCollection->addAttributeToFilter('designer',$attributes_designers);
}
if(count($attributes_colors)>0 and !in_array("ALL",$attributes_colors)) {        
    $_productCollection->addAttributeToFilter('color',$attributes_colors);
}
if(count($attributes_sizes)>0 and !in_array("ALL",$attributes_sizes)) {        
    $_productCollection->addAttributeToFilter('size',$attributes_sizes);
}
if(isset($_GET['order'])) $_productCollection->setOrder($this->getRequest()->getParam('order'), $this->getRequest()->getParam('dir'));

$_productCollection->load();

Unfortunately I’m unable to get the attribute colour and size working, since they are not set on the configurable product, but it’s children.

Does anyone have an idea how to get this working?

Thanks in advance

+1  A: 

Two things:

  • I assume you know this, but just to make sure: You can easily make any of your attributes sortable by editing the attribute in Catalog >> Attributes >> Manage Attributes, editing one and the bottom option is called "Used for Sorting in Product Listing" - which you should set to "Yes".
  • As you have found out, Magento isn't going to want to sort a configurable product by any values in the simple products that have been associated to it. This actually makes plenty of sense. If you are trying to sort by color, and you have a configurable product with 2 simple products, one of which has the color of "Apple Red" and the other with "Zealot Black", then how would it sort that? It simply cannot make logical sense to do what you are hoping. It only sorts based on attributes assigned to the configurable product.
Prattski
Prattski - I'm currently listing the attribute color, and I then want to show all items in a category, with the color "Brown". The problem lies that this color is defined on the child of the configurable product. Do you have any idea how to solve this? Thanks
kris
You can do that if you setup color as a filterable attribute (filterable with results). Magento will look at all of the colors of the simple products associated with your configurable product. If you set it up correctly, you'll see the color filter on the left side of the category view.
Prattski