0

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?

  • Do you want to call method from one block to another block? – Prashant Valanda Apr 21 '16 at 7:39
  • yes for example ListProduct getAddToCartPostParams – fefe Apr 21 '16 at 7:41
0

Use below code in your block:

$block =  $this->getLayout()->createBlock('your-block-classnmae');

$block->getAddToCartPostParams();
  • can you explain a bit please do I place this in template or class? what is actually doing? – fefe Apr 21 '16 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 Apr 21 '16 at 7:46
  • so I place in custom template the name of my block class TopSellers which is extending ` \Magento\Framework\View\Element\Template` but I get error undefined Block-Type – fefe Apr 21 '16 at 7:49
  • you need to use $this->getLayout()->createBlock('Modulename\Namespace\Block\TopSellers '); – Prashant Valanda Apr 21 '16 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.