I've used DI preferences to set an interface to use a class in my own module e.g.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\CatalogInventory\Api\StockManagementInterface" type="MySpace\MyModule\Model\StockManagement"/>

However, there is also a DI preference for this interface in the original core module.

In some cases this works, and others (such as in this case for Magento\CatalogInventory\Api\StockManagementInterface), it does not - it still uses the original.

What determines which class wins out? If it cannot be determined, how is this a reliable way to do a full class rewrite in the many many cases where Magento 2 has made it impractical to use a plugin or observer?

[Note that I "solved" the above issue by setting a preference for the class Magento\CatalogInventory\Model\StockManagement instead, but this does not allow for direct use of that class such as for composition]

1 Answer 1


All config files merged in order of module load.

You can change order by add sequence in you module.xml files.

For example

    <module name="Magento_Search"/>
    <module name="Magento_Catalog"/>

means that your module will load after Magento_Search and Magento_Catalog and you will overwrite preferences of these modules

  • But how does one handle the case of another module (unknown) coming later and defining its own preference overriding your module's? Do modules have a way of detecting their load order and so on?
    – George
    May 22, 2017 at 1:20
  • You cannot handle it. It is why we don't recommend use preferences for overwriting core functionality
    – KAndy
    May 22, 2017 at 17:49
  • Ok thank you. I dont know to what extent we can use "around" clause to override core observes "execute" function though. So a chicken n' egg situation. Been mentioned elsewhere.
    – George
    May 23, 2017 at 10:49
  • 1
    Protip: Execute bin/magento setup:upgrade afterwards for the changes to take effect. Apr 13, 2018 at 11:25

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.