0

I have two modules which are using the same block function.

i.e. module 1-> ABC, module 2-> XYZ

both modules are using preference method to override core module's block file.

=> Let say in core block file there is one function name getPrice() and it will return price. Both modules are overriding the same class function to manipulate the new price.

  • module ABC return price multiply by 2
  • module XYZ return price addition by 50

and I want both modules should work with its own extended function. How it can be work?

What solution do I need to apply it so both functions will work perfectly?

3
  • You wont to enable both modules at the same time? Commented Aug 9, 2019 at 6:49
  • Yes i want both modules enable on same time. Commented Aug 10, 2019 at 7:07
  • Then add both codes in a single module or use plugin(save after or around method). Commented Aug 10, 2019 at 7:34

2 Answers 2

3

First you need a conflict detector extension like this

https://github.com/magefan/module-conflict-detector

This will give you an idea on which extension has priority over your block.

Then you will have to rework the module that has current priority to extend the other module block logic.

Then if you are overriding the same method(s) you will have to merge logic from both modules into the single method of the module block logic that has priority.

It's not easy and it's not simple. I once worked on a store where the store owner kept purchasing random extensions. I had to deal with a scenario where a controller was overiden three times.

I now try to discourage third party extensions in initial development unless mission critical and defer niceties type extension to phase two of the build.

But also I've learnt to watch for extensions which override a lot and try to avoid using them again in future. Unfortunately that can include avoiding some of the free ones on git. It all depends on whether you can factor the cost of dealing with conflicts in the initial build.

The alternative is to deal with conflicts as part of paid support. But it's unlikely you are going to get sign off from the client if things are still broken in the first phase of the build.

0

It is not recommended to use the preferences except in rare case. Instead of preferences always prefer to create a plugin.

You can create a after plugin on a public function getPrice() in both the module. And assign the sort order to the plugin which you want to execute first.

Refer this to create plugin: https://www.mageplaza.com/magento-2-module-development/magento-2-plugin-interceptor.html

2
  • thanks @Mitro I know that we can use a plugin instead of preference but I purchased both modules and both used the same preference method. what should be the solution? Commented Aug 9, 2019 at 9:41
  • 1
    Okay.Please try with the given soultion : magento.stackexchange.com/questions/136852/…
    – Mitali
    Commented Aug 9, 2019 at 11:05

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.