1

I'm trying to achieve so that products with specific Manufacturer should be the first in the collection (for example, user wants to sort by price, so it should show all the products by Manufacturer X sorted by the price and then rest of the products like normally).

My idea was to hook somewhere between when the collection is being sorted by those attributes, but before it is "paginated". But I have trouble finding the place for it...

Of course if someone has a better idea, I'd love to hear it :)

1
  • See if this helps: magento.stackexchange.com/q/14664/146. It's about sorting a collection based on custom attributes. The idea is to use addExpressionAttributeToSelect to generate a virtual 1|0 flag and sort the collection first by that flag and then by the sort order requested by the user.
    – Marius
    Feb 18, 2015 at 14:26

1 Answer 1

0

Okay, so thanks to @Marius, I kinda figured it out:

<?php
class One_Two_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
    if (Mage::app()->getStore()->getId() == Mage_Core_Model_App::ADMIN_STORE_ID){
        return parent::addAttributeToSort($attribute, $dir);
    }

    if ($attribute == 'position' || $attribute == 'name' || $attribute == 'price') {
        // We need to have the ocean wheel products as the first ones.
        // First get the ids
        $ids = $this->_getIdsIWantFirst();

        // Create sorting sql
        $order = new Zend_Db_Expr("CASE WHEN e.entity_id IN ('" . implode("', '", $ids) . "') THEN 0 ELSE 1 END");

        // Work the magic
        $this
            ->getSelect()
            ->order($order);

        return $this;
    }

    else {
        return parent::addAttributeToSort($attribute, $dir);
    }
}

Now I just need to figure out how to still include the original sorting within those wanted ids...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.