Ravichandran Jothi
Ravichandran Jothi

Reputation: 3066

addAttributeToSort() not working in magento Product collection?

I have added custom fields in products table in magento. I want to sort use that custom field but it won't. I don't know what is the issue?.

    $_cproduct = Mage::getmodel('catalog/product');
    $_productCollection = $_cproduct->getCollection();
    $_productCollection->addAttributeToSelect('name')->addAttributeToSelect('expired')->addAttributeToSelect('dealcategory')->addAttributeToSelect('dealcity')->addAttributeToSelect('deal_position')->addAttributeToSelect('special_to_date')->addAttributeToSelect('title')->setStore(Mage::app()->getStore()->getId())->addStoreFilter(Mage::app()->getStore()->getId())->addAttributeToSort('entity_id', 'DESC')->addAttributeToSort('dealcategory', 'DESC');
    $_productCollection->addAttributeToFilter('status', 1)->addAttributeToFilter('expired', 0);


$_productCollection->addAttributeToFilter('dealcategory', array('in' => array(129, 135, 136, 137, 141, 156, 205)));


    $now = Mage::getModel('core/date')->timestamp(time());
    $dateStart = date('Y-m-d' . ' 00:00:00', $now);    

    $_productCollection->addFieldToFilter('special_to_date', array('from' => $dateStart));
    $dealcity = Mage::app()->getRequest()->getParam('dealcity', false);
    $_productCollection->addAttributeToSort('deal_position', 'ASC');


    echo "<pre>";
    print_r($_productCollection->getData());
    echo "</pre>";

But every time I got unusual order like below, I want ot sort based on "deal_position"

Array
(
    [0] => Array
        (
            [entity_id] => 2568
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => virtual
            [sku] => comedybar7/2/12
            [created_at] => 2012-07-02 18:36:22
            [updated_at] => 2012-10-16 09:52:41
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side travel deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 135
            [special_to_date] => 2012-10-25 00:00:00
            [deal_position] => 4
        )

    [1] => Array
        (
            [entity_id] => 2566
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => virtual
            [sku] => livingwellfinal
            [created_at] => 2012-06-28 14:48:34
            [updated_at] => 2012-10-16 09:51:58
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-11-30 00:00:00
            [deal_position] => 1
        )

    [2] => Array
        (
            [entity_id] => 2565
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => simple
            [sku] => laser-toenail-10-toes
            [created_at] => 2012-06-27 21:10:19
            [updated_at] => 2012-10-16 09:52:23
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-11-30 00:00:00
            [deal_position] => 3
        )

    [3] => Array
        (
            [entity_id] => 2564
            [entity_type_id] => 10
            [attribute_set_id] => 9
            [type_id] => simple
            [sku] => laser-toenail-5-toes
            [created_at] => 2012-06-27 21:10:19
            [updated_at] => 2012-10-16 09:52:10
            [has_options] => 0
            [required_options] => 0
            [dealcategory_value] => Side Deal
            [status] => 1
            [expired] => 0
            [dealcategory] => 129
            [special_to_date] => 2012-12-31 00:00:00
            [deal_position] => 2
        )

)

I used addOrder() that also returns the same output. What is the issue?

Upvotes: 1

Views: 7594

Answers (1)

Slayer Birden
Slayer Birden

Reputation: 3694

It seems you've got lost in your own code. You've added 3! sorts in your code:

line3: ...->addAttributeToSort('entity_id', 'DESC')->addAttributeToSort('dealcategory', 'DESC');
line10: $_productCollection->addAttributeToSort('deal_position', 'ASC');

Make sure you have only sort that you need. Also, do not use addAttributeToSelect multiple times, use it once, but pass an array as an argument:

$_productCollection->addAttributeToSelect(array('name', 'expired', ...));

Upvotes: 3

Related Questions