Magento sorting related products by price

2019-09-17 04:04发布

问题:

i've been trying to sort related products by price in descending oreder (cheapest at the top and expensive at bottom) without any luck so far, maybe someone could guide me on how to do that?

回答1:

See this file app\code\core\Mage\Catalog\Model\Product.php and check this function line 814

public function getRelatedProductCollection()
{
    $collection = $this->getLinkInstance()->useRelatedLinks()
        ->getProductCollection()
        ->setIsStrongMode();
    $collection->setProduct($this);
    return $collection;
}

You need to modify this function by extend this Model Class so first Create an Module and Write this function inside your function. and add Order to collection.

$collection->setOrder('price', 'DESC');


public function getRelatedProductCollection()
{
    $collection = $this->getLinkInstance()->useRelatedLinks()
        ->getProductCollection()
        ->setIsStrongMode();
    $collection->setProduct($this);
    $collection->setOrder('price', 'DESC');
    return $collection;
}

You need to Extend this Module Mage_Catalog_Model_Product and also modify this function



回答2:

Go to /app/code/core/Mage/Catalog/Model/ path and add the below code in Product.php

public function getRelatedProductCollection()
    {
$collection = $this->getLinkInstance()->useRelatedLinks()
        ->getProductCollection()
        ->setIsStrongMode();
    $collection->setProduct($this);
    $collection->setOrder('price', 'DESC');
    return $collection;
    }

You can also use this for weight. Just write 'weight' in place of 'price'. For ascending just write 'ASC' in place of 'DESC' .