Add sort by position to custom product relation In magento

106 Views Asked by At

HI i have added a custom product relation in magento .And i am showing the collection of that relation from below function on custom controller using the catalog list template.

protected function _getProductCollection()
{
    if($_GET['product_id'])
    {
    if(isset($_GET['p'])){
    $curpage=  $_GET['p'];  
    }else{
    $curpage = 1 ;}
    if(isset($_GET['limit'])){
    $limit=  $_GET['limit'];  
    }else{
    $limit = 12 ;}
$productid=$_GET['product_id'];
        $product = Mage::getModel('catalog/product_link')
        ->getCollection()
        ->addFieldToFilter('link_type_id', 6)
        ->addFieldToFilter('product_id',$productid)
        ->load();
        $LinkedProduct=$product->getData();
        foreach($LinkedProduct as $LinkedProducts)
        { $LinkedProductId[]= $LinkedProducts['linked_product_id']; }
        $productIds = array_values($LinkedProductId);
        $_productCollection=Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
        ->addAttributeToFilter('entity_id', array('in' => $productIds))
        ->addAttributeToSort(trim($_GET['order']),$_GET['dir'])
        ->setCurPage($curpage)
        ->setPageSize($limit)
        ->load();
        //echo $_productCollection->getSelect() ; 
        $this->_productCollection=$_productCollection;
}
    return $this->_productCollection;
}

But the sort by position is not working. Can you please suggest me how can i achieve this.

1

There are 1 best solutions below

0
On

According to Mage_Catalog_Model_Resource_Product_Collection, there is a setOrder($attribute, $dir = 'desc') method which could do the job.

By the way, you should really not use $_GET on Magento, prefer

$this->getRequest()->getParam('your-param-key', 'any default value if wanted')