Sorting products into categories (in the parent category view)

I have a rather unique problem.

I have a store where there are several categories in the settings like

Collection
.... Shorts (products: small 16 - RED and small 20 - BLUE)
.... Dress (products: blue: 16, green 19)

If I open the collection in the store, I will get items like this

Blue 16
Green 19
small 16 - RED
small 20 - BLUE

I want my output to be like this:

small 16 - RED
small 20 - BLUE
Blue 16
Green 19

How can I get these results? I'm sorry that I did not provide any code, since I have no idea how I should achieve this.

+8
php e-commerce magento
source share
3 answers

1 create an observer in the catalog_block_product_list_collection event

<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 create class 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'); } } 

here we set the sorting by the name of the child category, you can change it to the category identifier or add an attribute of any category to the collection and sort by this attribute

 ->order('child_category_name DESC'); 

this is just an example of how to quickly sort a collection of products by child categories, of course, you can add a dynamic contribution to the toolbar and sort the collection

0
source share

I am doing something similar to this.

In Magento admin, you can manually set the order in which products are displayed on the category page.

  • Catalog → Category Management (select your category)
  • On the tab "Product Category" you will see a table containing all the products assigned to the category, in the far right corner is the column "Position". Here you enter the value int, the smaller the number, the higher the product will be displayed on the category page.
+1
source share

I think you should create an attribute from admin,

Create the custom_order attribute from Admin-> Catalog-> Attributes-> Manage Attributes .

set Used in product listing = Yes Used for sorting in product listing = Yes

Assign a position value for each product individually.

Then go to Admin-> Catalog-> Manage Categories .

select a category, click the "Display Settings" tab,

set "default product list" custom_order

0
source share

All Articles