1

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?

asked Sep 27, 2019 at 16:21

1 Answer 1

0

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.

answered Sep 29, 2019 at 16:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.