1

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){
    if($customer):
        $wishlists = Mage::getModel('wishlist/wishlist')->loadByCustomer($customer, true);
        if($wishlists->getItemCollection()->getSize()):
            $wishlists = $this->favorites->getItemCollection();
            $wishlists->addWishListSortOrder('added_at', 'DESC');
            $wishlists->getSelect()->limit(10, 0);
            return $wishlists;
        endif;
        return;
    endif;
    return;
}

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. – Joseph at SwiftOtter Sep 5 '16 at 13:36
0

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

app/design/frontend/rwd/default/template/wishlist/item/list.phtml

0
$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')
                ->addAttributeToSelect('name');

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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