I wanted to add some extra custom methods to the block classes:

  • Related
  • Upsell
  • Cross-sell

Each of these extends the Magento\Catalog\Block\Product\AbstractProduct

But using di.xml preference I was not able to overwrite the abstract class, maybe because Magento does not allow that.

I wanted to know if there is a way to add the common functionality to each of these blocks, without individually overwrite each of them and repeating the same code.

  • Magento\Catalog\Block\Product\AbstractProduct is not an abstract class,, it's just simple class . – Keyur Shah Jul 12 '16 at 6:19
  • @KeyurShah, Yes I know. I was still not able to overwrite/extend this class using di.xml <preference/>. I was thinking maybe like in Magento1 we could not <rewrite/> the class having Abstract in their class name does same apply for Magento2 as well. – Atish Goswami Jul 12 '16 at 16:23

Yes, you can't override classes having "Abstract" keyword in Magento. Alternatively, you can override the class \Magento\Catalog\Block\Product\View as it is the child class of \Magento\Catalog\Block\Product\AbstractProduct, it should possesses all the property's and functions of it's base class.

| improve this answer | |

You cannot overwrite abstract classes. Individual classes directly extend their respective parent classes using the extends keyword.

Seeing as this is PHP native code and is therefore not handled by Magento's Object Manager there is no way to hook into this.

Regards, Vincent

| improve this answer | |

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.