I need to override the class \Magento\Catalog\Model\Product\Link using preference, which is working fine,but the above mentioned class has a proxy class generated automatically \Magento\Catalog\Model\Product\Link\Proxy, which does not include the new methods added in the override. Is there any way we can inject those methods in the proxy class?


I have tried to dig in to this and found that we can inject proxy using di.xml. But when i tried to do that, i got an exception that the class Package\Module\Model\Class\Proxy Does not exist. That means the new proxy i am trying to generate is not being generated automatically.

  • Taral, is your Magento instance set into developer mode? If not, you need to compile di in order to have proxy generated. – Ivan Chepurnyi May 16 '16 at 11:30
  • Why do you add methods to this class? – KAndy May 16 '16 at 11:46
  • Ivan, my magento instance is in developer mode – Taral Patoliya May 16 '16 at 12:03
  • @KAndy I need to add the exact functionality as related products, so in order to do that, i need to add some methods for the new link type. as i have noticed every link type (related,cross_sell,up-sell) has their own methods in that class. – Taral Patoliya May 16 '16 at 12:05
  • can't you just create the proxy class by hand and set in di.xml a preference for \Magento\Catalog\Model\Product\Link\Proxy to Package\Module\Model\Class\Proxy ? – Marius May 18 '16 at 6:13

After reading comments in which you've stated:

I have done similar thing and it worked. I have settled with that solution. but i wanted to generate a new proxy class which had new methods included. As the proxy class is used to reduce the load of initializing the class. now as the proxy class is no longer used, the original class will take some more server load. but i think its negligible.

The whole problem you are trying to solve, can be done in more elegant and less upgrade painful way.

If you just added new methods to existing implementation it means you are not changing behavior of original class

  1. You should avoid extending public methods of existing class in favor of creating your own class that uses original one as a dependency
  2. Then pass your class in the place where you were using original one

This gives you such side benefits:

  1. You code easier to test it (single dependency over mocking all core dependencies)
  2. It does not violate single responsibility principle
  3. It does not affect core in places you don't expect
  4. It decouples your business logic from core implementation
| improve this answer | |

I managed to generate the correct Proxy.php file that included my new function by adding the following to my module di.xml

<type name="Magento\Catalog\Model\Product">
        <argument name="productLink" xsi:type="object">Package\Module\Model\Product\Link\Proxy</argument>

Thanks @minlare for the help

| improve this answer | |

Your Answer

