I want to add the toolbar from the product list to my wishlist to have the pagination and the sorter element.
So in my wishlist_index_index.xml I added the block Magento\Catalog\Block\Product\ProductList\Toolbar to the customer.wishlist.
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<referenceBlock name="customer.wishlist">
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="product_list_toolbar" template="Magento_Catalog::product/list/toolbar.phtml">
<block class="Magento\Theme\Block\Html\Pager" name="product_list_toolbar_pager"/>
</block>
<action method="setToolbarBlockName">
<argument name="name" xsi:type="string">product_list_toolbar</argument>
</action>
</referenceBlock>
</referenceContainer>
</body>
</page>
Also I copied some methods that are required for the toolbar from Magento\Catalog\Block\Product\ListProduct to my wishlist Block which extends the default one. As you can see I changed the use statement Magento\Catalog\Model\ResourceModel\Product\Collection to Magento\Wishlist\Model\ResourceModel\Item\Collection to match my needs.
<?php
namespace CustomVendor\Wishlist\Block\Customer;
use Magento\Catalog\Block\Product\ProductList\Toolbar;
use Magento\Wishlist\Model\ResourceModel\Item\Collection;
class Wishlist extends \Magento\Wishlist\Block\Customer\Wishlist
{
/**
* Default toolbar block name
*
* @var string
*/
protected $_defaultToolbarBlock = Toolbar::class;
/**
* Wishlist constructor.
*
* @param \Magento\Catalog\Block\Product\Context $context
* @param \Magento\Framework\App\Http\Context $httpContext
* @param \Magento\Catalog\Helper\Product\ConfigurationPool $helperPool
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer
* @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
* @param \Magento\Theme\Block\Html\Pager $pager
* @param array $data
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Framework\App\Http\Context $httpContext,
\Magento\Catalog\Helper\Product\ConfigurationPool $helperPool,
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
\Magento\Framework\Data\Helper\PostHelper $postDataHelper,
array $data = []
) {
parent::__construct(
$context,
$httpContext,
$helperPool,
$currentCustomer,
$postDataHelper,
$data
);
}
/**
* Add wishlist conditions to collection
*
* @param \Magento\Wishlist\Model\ResourceModel\Item\Collection $collection
* @return $this
*/
protected function _prepareCollection($collection)
{
$collection
->setInStockFilter(true)
->setOrderByProductName('asc');
return $this;
}
/**
* Preparing global layout
*
* @return void
*/
protected function _prepareLayout()
{
parent::_prepareLayout();
$this->pageConfig->getTitle()->set(__('My Wish List'));
}
/**
* Before rendering html, but after trying to load cache
*
* @return $this
*/
protected function _beforeToHtml()
{
$collection = $this->getWishlistItems();
$this->addToolbarBlock($collection);
$collection->load();
return $this;
}
/**
* Add toolbar block from product listing layout
*
* @param Collection $collection
*/
private function addToolbarBlock(Collection $collection)
{
$toolbarLayout = $this->getToolbarFromLayout();
if ($toolbarLayout) {
$this->configureToolbar($toolbarLayout, $collection);
}
}
/**
* Retrieve Toolbar block from layout or a default Toolbar
*
* @return Toolbar
*/
public function getToolbarBlock()
{
$block = $this->getToolbarFromLayout();
if (!$block) {
$block = $this->getLayout()->createBlock($this->_defaultToolbarBlock, uniqid(microtime()));
}
return $block;
}
/**
* Get toolbar block from layout
*
* @return bool|Toolbar
*/
private function getToolbarFromLayout()
{
$blockName = $this->getToolbarBlockName();
$toolbarLayout = false;
if ($blockName) {
$toolbarLayout = $this->getLayout()->getBlock($blockName);
}
return $toolbarLayout;
}
/**
* Retrieve list toolbar HTML
*
* @return string
*/
public function getToolbarHtml()
{
return $this->getChildHtml('toolbar');
}
/**
* Configures the Toolbar block with options from this block and configured product collection.
*
* The purpose of this method is the one-way sharing of different sorting related data
* between this block, which is responsible for product list rendering,
* and the Toolbar block, whose responsibility is a rendering of these options.
*
* @param \Magento\Catalog\Model\Product\ProductList\Toolbar $toolbar
* @param Collection $collection
* @return void
*/
private function configureToolbar(Toolbar $toolbar, Collection $collection)
{
// use sortable parameters
$orders = $this->getAvailableOrders();
if ($orders) {
$toolbar->setAvailableOrders($orders);
}
$sort = $this->getSortBy();
if ($sort) {
$toolbar->setDefaultOrder($sort);
}
$dir = $this->getDefaultDirection();
if ($dir) {
$toolbar->setDefaultDirection($dir);
}
$modes = $this->getModes();
if ($modes) {
$toolbar->setModes($modes);
}
// set collection to toolbar and apply sort
$toolbar->setCollection($collection);
$this->setChild('toolbar', $toolbar);
}
}
When I load the site I get the following error:
[08-May-2018 15:52:25 UTC] PHP Fatal error: Uncaught Error: Call to undefined method Magento\Wishlist\Model\ResourceModel\Item\Collection::addAttributeToSort() in /Applications/MAMP/htdocs/mag2/vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php:196
Stack trace:
#0 /Applications/MAMP/htdocs/mag2/app/code/CustomVendor/Wishlist/Block/Customer/Wishlist.php(206): Magento\Catalog\Block\Product\ProductList\Toolbar->setCollection(Object(Magento\Wishlist\Model\ResourceModel\Item\Collection))
#1 /Applications/MAMP/htdocs/mag2/app/code/CustomVendor/Wishlist/Block/Customer/Wishlist.php(127): CustomVendor\Wishlist\Block\Customer\Wishlist->configureToolbar(Object(Magento\Catalog\Block\Product\ProductList\Toolbar), Object(Magento\Wishlist\Model\ResourceModel\Item\Collection))
#2 /Applications/MAMP/htdocs/mag2/app/code/CustomVendor/Wishlist/Block/Customer/Wishlist.php(110): CustomVendor\Wishlist\Block\Customer\Wishlist->addToolbarBlock(Object(Magento\Wishlist\Model\ResourceModel\Item\Collection))
#3 /Applications/MAMP/htdocs/mag2/vendor/magento/framew in /Applications/MAMP/htdocs/mag2/vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php on line 196
This is because I use another collection. How would you make any progress here? Or is there another way to include the toolbar that you would to prefer?