I want to show a recently added wishlist to be displayed at the starting In wishlist Page in magento2? Any help would be appreciated.
2 Answers
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!
-
-
Can you please confirm any other block is not conflicting here which override this same block ? Magento\Wishlist\Block\Customer\Wishlist Nov 15, 2019 at 6:56
-
Or you can use echo die in _prepareCollection() function and check. Nov 15, 2019 at 6:56
-
1
-
_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.