Having a custom Block where I fetch custom data what is the accepted workaround to inject the methods in this custom Block from another block.
Having the following situation I fetch in my custom block the most viewed products I would like to reuse the methods and template from ListProducts. Is there a way to do this without extending the above class?
1 Answer 1
Use below code in your block:
$block = $this->getLayout()->createBlock('your-block-classnmae');
$block->getAddToCartPostParams();
 answered Apr 21, 2016 at 7:42
 
 
 
 Prashant Valanda 
 
 12.7k5 gold badges44 silver badges70 bronze badges
 
 - 
 can you explain a bit please do I place this in template or class? what is actually doing?fefe– fefe2016年04月21日 07:44:36 +00:00Commented Apr 21, 2016 at 7:44
- 
 Yes you can place it in template file.It will create a block instance, you can access block method using it.Prashant Valanda– Prashant Valanda2016年04月21日 07:46:09 +00:00Commented Apr 21, 2016 at 7:46
- 
 so I place in custom template the name of my block classTopSellerswhich is extending ` \Magento\Framework\View\Element\Template` but I get errorundefined Block-Typefefe– fefe2016年04月21日 07:49:06 +00:00Commented Apr 21, 2016 at 7:49
- 
 you need to use$this->getLayout()->createBlock('Modulename\Namespace\Block\TopSellers ');Prashant Valanda– Prashant Valanda2016年04月21日 08:39:24 +00:00Commented Apr 21, 2016 at 8:39
default
 
 
 
getAddToCartPostParams