In catalog_category_view.xml
<referenceContainer name="content">
<block class="Magento\Catalog\Block\Product\ProductList\Toolbar" name="my-block-name" template="Vendor_Module::contents.phtml"/>
</referenceContainer>
In content.phtml
/**
* Product list toolbar
*
* @var $toolBarBlock \Magento\Catalog\Block\Product\ProductList\Toolbar
*/
print_r(toolBarBlock->getTotalNum());
I get error:
"PHP message: PHP Fatal error: Uncaught Error: Call to a member
function getSize() on null in
.../htdocs/vendor/magento/module-catalog/Block/Product/ProductList/Toolbar.php:609
Line 609:
/**
* @return int
*/
public function getTotalNum()
{
return $this->getCollection()->getSize();
}
Not sure what I am missing anyone any idea?
1 Answer 1
Seems like your toolbar has no collection, but it is required. In the original magento layout the Toolbar block rendered inside the Magento\Catalog\Block\Product\ListProduct block, which access toolbar from the layout and set the product collection:
vendor/magento/module-catalog/Block/Product/ListProduct.php
/**
* Need use as _prepareLayout - but problem in declaring collection from
* another block (was problem with search result)
* @return $this
*/
protected function _beforeToHtml()
{
$collection = $this->_getProductCollection();
$this->addToolbarBlock($collection);
$collection->load();
return parent::_beforeToHtml();
}
I think you should declare\create own toolbar block and rewrite the getCollection() method, or set it manually before layout render.