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?

asked Apr 21, 2016 at 7:36
2
  • Do you want to call method from one block to another block? Commented Apr 21, 2016 at 7:39
  • yes for example ListProduct getAddToCartPostParams Commented Apr 21, 2016 at 7:41

1 Answer 1

0

Use below code in your block:

$block = $this->getLayout()->createBlock('your-block-classnmae');
$block->getAddToCartPostParams();
answered Apr 21, 2016 at 7:42
4
  • can you explain a bit please do I place this in template or class? what is actually doing? Commented 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. Commented Apr 21, 2016 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 Commented Apr 21, 2016 at 7:49
  • you need to use $this->getLayout()->createBlock('Modulename\Namespace\Block\TopSellers '); Commented Apr 21, 2016 at 8:39

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.