How can I sort products in the wishlist page by price asc and desc. I have found a piece of code but it is not working any help please.

public function getWishlistCollection(Mage_Customer_Model_Customer $customer = null){
        $wishlists = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
            $wishlists = $this->favorites->getItemCollection();
            $wishlists->addWishListSortOrder('added_at', 'DESC');
            $wishlists->getSelect()->limit(10, 0);
            return $wishlists;

this code can be found.

  • As a sidenote, I recommend using squiggly braces for any non-templating code. This is the Magento standard, and to me it is easier to read. Sep 5, 2016 at 13:36

3 Answers 3


The price is added to collection items in afterload method, so you can't order by item with the collection methods.

But you can retrieve an array of items from the collection, sort this array as you want and then use this array instead of the collection in your template.

Go ahead and try this code, it worked for me:

$collection = $this->getItems();
$arrayOfItems = $collection->getItems();

function sortWishlistItems($a, $b)
if($a->price == $b->price)
return 0;
return $a->price > $b->price ? 1 : -1;

usort($arrayOfItems, 'sortWishlistItems');

and then use the $arrayOfItems instead of this->getItems();

You can apply the above code here


$customer = Mage::getSingleton('customer/session')->getCustomer();
        $wishList = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer);
        $wishListItemCollection = $wishList->getItemCollection();

        if (count($wishListItemCollection)) {
            $arrProductIds = array();
            foreach ($wishListItemCollection as $item) {
                $arrProductIds[] = $item->getProductId();
        $productsCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToFilter('entity_id', array('in' => $arrProductIds))
                ->addAttributeToSort('price', 'DESC')

        foreach ($productsCollection as $product) {
            echo $product->getname();

To sort products in a wishlist by price in Magento 2, you can follow these steps:

  1. First, create a module in Magento that will override the wishlist controller. You can do this by creating a new module or using an existing one.

  2. In your module's di.xml file, add the following code to override the wishlist controller:

In this example, we have created a plugin of type "Magento\Wishlist\Controller\Index\Index" and named it "sort_wishlist_items_by_price". We have set the plugin's sortOrder to 1 to ensure that it is executed before other plugins.

  1. Next, create a PHP file named "SortByPrice.php" in your module's "Plugin/Wishlist" directory with the following code:

namespace Vendor\Module\Plugin\Wishlist;

class SortByPrice { public function beforeExecute( \Magento\Wishlist\Controller\Index\Index $subject ) { $collection = $subject->getWishlist()->getItemCollection();

            ['product' => $collection->getTable('catalog_product_entity')],
            'main_table.product_id = product.entity_id',
            ['product_price' => 'product.price']
        ->order('product_price ASC');

    return [];


In this example, we have created a plugin named "SortByPrice" with a function named "beforeExecute". This function retrieves the wishlist item collection and joins it with the product table to get the product price. It then sorts the collection by product price in ascending order.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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