Home » Php » php – Sort products by category (in parent category view)

php – Sort products by category (in parent category view)

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a quite unique problem.

I have a shop where there are multiple categories in a setup like this

Collection
…. Shorts (products: small 16 – RED and small 20 – BLUE)
…. Dress (products: blue: 16 , Green 19)

If I open Collection in the shop I get the 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 this results? I’m sorry i haven’t provided any code, as i have no idea how i should achieve this

How to&Answers:

1 create observer on 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 collection sort by child category name, you can change it to category id or add to collection any category attribute and sort by this attribute

->order('child_category_name DESC');

this is just sample how quickly sort product collection by child categories, of course you can add option in toolbar and sort collection dynamically

Answer:

I do something similar to this.

Within Magento admin you can manually set the order in which the products display on the category page.

  • Catalog -> Manage Categories (select your category)
  • Under the “Category Products” tab you will see a table containing all the products assigned to the category, on the far right there is a column called “Position”. Here is where you enter an int value, the lower the number the higher the product would appear on the category page.

Answer:

I think you should create an attribute from admin,

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

set Used in Product Listing = Yes
Used for Sorting in Product Listing = Yes

Assign the position value for each product Individually .

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

select a category, click on Display settings tab,

set “Default Product Listing Sort By” custom_order