1 utworzyć obserwatora na razie catalog_block_product_list_collection
<events>
<catalog_block_product_list_collection>
<observers>
<namespace_module>
<class> namespace_module/observer</class>
<method>collectionList</method>
</namespace_module >
</observers>
</catalog_block_product_list_collection>
</events>
2 utworzyć klasy Namespace_Module_Model_Observer
class Namespace_Module_Model_Observer
{
public function collectionList($observer)
{
/** @var Mage_Catalog_Model_Category $currentCategory */
$currentCategory = Mage::registry('current_category');
$children = Mage::getResourceModel('catalog/category')->getChildrenIds($currentCategory);
if (!$children) {
return $this;
}
$children = implode(',', $children);
/** @var Mage_Catalog_Model_Resource_Product_Collection $collection */
$collection = $observer->getCollection();
$attr = $this->_getAttribute('name');
$collection->getSelect()
->join(
array('c' => $this->_getResource()->getTableName('catalog_category_product')),
"c.product_id = e.entity_id AND c.category_id IN ($children)",
array('child_category_id' => 'category_id')
)
->join(
array('ac' => $this->_getResource()->getTableName('catalog_category_entity_' . $attr['backend_type'])),
"c.category_id = ac.entity_id AND ac.attribute_id = {$attr['attribute_id']}",
array('child_category_name' => 'value')
)
->order('child_category_name DESC');
return $this;
}
protected function _getAttribute($attributeCode, $static = true, $entityTypeId = 3)
{
$readAdapter = $this->_getReadAdapter();
$select = $readAdapter->select()
->from($this->_getResource()->getTableName('eav/attribute'))
->reset(Zend_Db_Select::COLUMNS)
->columns(array('attribute_id', 'backend_type'))
->where('entity_type_id = ?', $entityTypeId)
->where('attribute_code = ?', $attributeCode)
->limit(1);
if (!$static) {
$select->where('backend_type != ?', 'static');
}
$entityId = $readAdapter->query($select)->fetch();
return $entityId;
}
protected function _getResource()
{
return Mage::getSingleton('core/resource');
}
protected function _getReadAdapter()
{
return $this->_getResource()->getConnection('core_read');
}
}
tu zestaw kolekcja Sortuj według nazw kategorii dziecko, można go zmienić na kategorii ID lub dodać do zbieraj wszelkie atrybuty kategorii i sortuj według tego atrybutu:
->order('child_category_name DESC');
to tylko przykład, jak szybko sortować zbiór produktów według kategorii podrzędnych, oczywiście można dodać opcję na pasku narzędzi i sortować kolekcje dynamicznie
I jak/gdzie powinienem utworzyć kod do tego sortowania? –
Nie musisz do ciebie pisać. otrzymanie domyślnie wyniku w porządku rosnącym. – Supravat
Musiałem być zmęczony, gdy zobaczyłem, co odpowiedziałeś - przepraszam. Wydaje się, że dużo pracy po zmianie produktów, musi istnieć sposób, aby to zrobić z niestandardowym atrybutem lub czymś bardziej niestandardowym –