Question

I want to show a recently added wishlist to be displayed at the starting In wishlist Page in magento2? Any help would be appreciated.

Was it helpful?

Solution

You can create di.xml file in your custom module here

app/code/Vendor/Module/etc/di.xml

Content for this file is..

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Wishlist\Block\Customer\Wishlist" type="Vendor\Module\Block\Customer\Wishlist" />
</config>

Now we need to create one Block file here in custom module

app/code/Vendor/Module/Block/Customer/Wishlist.php

Content for this file is..

<?php
namespace Vendor\Module\Block\Customer;

class Wishlist extends \Magento\Wishlist\Block\Customer\Wishlist
{
    protected function _prepareCollection($collection)
    {
        $collection->setInStockFilter(true)->setOrder('added_at', 'DESC');
        return $this;
    }
}

Hope this will help you!

OTHER TIPS

_prepareCollection method is not overriding for some reason. So overrided a public function "getWishlistItems" which calls the protected function "_prepareCollection" . Also need to override the private method which is called insided the "getWishlistItems" function. In that way i can able to achieve it. So the required functions inside the above suggested class content by Kishan will be

public function getWishlistItems()
{
    if ($this->_collection === null) {
        $this->_collection = $this->_createWishlistItemCollection();
        $this->_prepareCollection($this->_collection);
        $this->paginateCollection();
    }
    return $this->_collection;
}

private function paginateCollection()
{
    $page = $this->getRequest()->getParam("p", 1);
    $limit = $this->getRequest()->getParam("limit", 10);
    $this->_collection
        ->setPageSize($limit)
        ->setCurPage($page);
}

protected function _prepareCollection($collection)
{
    $collection->setInStockFilter(true)->setOrder('added_at', 'DESC');
    return $this;
}

It works for me. Hope this will help someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top