Rohit Goel
Rohit Goel

Reputation: 3554

sorting and pager not working on custom collection Magento

Hi i have added a new product relation in magento named as accesories.

Like i have a main product A and i have attach B c and d with it as accesories

Now on the frontend i need to show this collection as the same design on list .phtml

so i have decide to render the custom collection on the list .phtml

for this i have render a custom collection in _getProductCollection()

by below code

if($_GET['product_id'])
        {

            $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))
            ->load();
            $this->_productCollection=$_productCollection;

    }else{




        if (is_null($this->_productCollection)) {
            $layer = $this->getLayer();
            /* @var $layer Mage_Catalog_Model_Layer */
            if ($this->getShowRootCategory()) {
                $this->setCategoryId(Mage::app()->getStore()->getRootCategoryId());
            }

            // if this is a product view page
            if (Mage::registry('product')) {
                // get collection of categories this product is associated with
                $categories = Mage::registry('product')->getCategoryCollection()
                    ->setPage(1, 1)
                    ->load();
                // if the product is associated with any category
                if ($categories->count()) {
                    // show products from this category
                    $this->setCategoryId(current($categories->getIterator()));
                }
            }

            $origCategory = null;
            if ($this->getCategoryId()) {
                $category = Mage::getModel('catalog/category')->load($this->getCategoryId());
                if ($category->getId()) {
                    $origCategory = $layer->getCurrentCategory();
                    $layer->setCurrentCategory($category);
                    $this->addModelTags($category);
                }
            }
            $this->_productCollection = $layer->getProductCollection();

            $this->prepareSortableFieldsByCategory($layer->getCurrentCategory());

            if ($origCategory) {
                $layer->setCurrentCategory($origCategory);
            }
        }
}
        return $this->_productCollection;
    }

The collection is working fine

but the problem is the sorting and toolbar is not working

. Can you please suggest me how can i make a sorting and pager works with my custom colection

Upvotes: 1

Views: 1076

Answers (1)

Rohit Goel
Rohit Goel

Reputation: 3554

OK i have solve this by adding a sort function in collection like below code

    $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($_GET['order'],$_GET['dir'] )
    ->setPageSize($_GET['limit'])
    ->load();
    echo $_productCollection->getSelect() ; 
    $this->_productCollection=$_productCollection;

the sorting was not working because there wqas no sort function added in the colection

->addAttributeToSort($_GET['order'],$_GET['dir'] )
        ->setPageSize($_GET['limit'])

now it is working fine.

thanks

Upvotes: 1

Related Questions