2

1) Use preference in di.xml and add new methods or do changes

2) Or create a new class which extends the actual class and attach the new class to your phtml.

Lets suppose you want to extend Magento\Catalog\Block\Product\Listproduct then you create a class like Vendor\Module\Block\Product\Listproduct and change here

<block class="Vendor\Module\Block\Product\Listproduct" name="category.products.list" as="product_list" template="Magento_Catalog::product/list.phtml">
1
  • i will go with the second approach. Its much more flexible. Plugins wont work here as i needed a new method. Commented Feb 12, 2019 at 10:39

2 Answers 2

0

Rather than extend block use Plugin

There is three type of plugin

  • Before plugin

  • After plugin

  • Around plugin

For more info please refer link

0

The best way to override a block would be a plugin, but note that a plugin can not be used on following:

  1. Final methods
  2. List item
  3. Non-public methods
  4. Class methods (such as static methods)
  5. __construct
  6. Virtual types
  7. Objects that are instantiated before Magento\Framework\Interception is bootstrapped.

Reference - https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html

Your Answer

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

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