Magento 2.1 proxy with di preference
-
03-10-2020 - |
Question
I am adding another type of related products. I am adding a new method to Magento\Catalog\Model\Product\Link class by way of di.xml preference. The method also does appear in the interceptor class.
However, when I follow the execution, this call is made:
$this->_linkInstance = $productLink;
Where $productLink is returning type Magento\Catalog\Model\Product\Link\Proxy
When I look in this class at run time there is no reference to my method like there is for other relations:
public function useRelatedLinks()
{
return $this->_getSubject()->useRelatedLinks();
}
Solution
I fixed my issue above by extending the proxy class itself by including the following:
In di.xml add this preference:
<preference for="Magento\Catalog\Model\Product\Link\Proxy"
type="SprintIntercom\RelatedDownloads\Model\Catalog\Product\Link\Proxy"/>
Then created a class in my custom module at Vendor\Module\Model\Catalog\Product\Link\Proxy.php
class Proxy extends \Magento\Catalog\Model\Product\Link\Proxy
And included the method:
/**
* {@inheritdoc}
*/
public function useCustomLinks()
{
return $this->_getSubject()->useCustomLinks();
}