How to override third party module \view\frontend\templates\ from my custom theme using prefrence?
-
15-04-2021 - |
Pergunta
I am using a third-party module, I am added one extra feature to it, after upgrade the extension all my changes removed right, how to handle this situation, i mean how to override third-party module from my theme,
eg. I am updated one feature in model app\code\Magefan\Blog\Model\Post.php How to add this update from my custom theme.
Solução
To achieve this you should override that class from your own module.
Create a Magento 2 module (lear here: https://devdocs.magento.com/videos/fundamentals/create-a-new-module/) and then add this in your etc/di.xml
<?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="Magefan\Blog\Model\Post" type="NameSpace\YourModule\Model\Magefan\Post" />
</config>
Now your class NameSpace\YourModule\Model\Magefan\Post.php
<?php
namespace NameSpace\YourModule\Model\Magefan;
class Post extends \Magefan\Blog\Model\Post {
public function yourNewFunction()
{
return true;
}
}
Now, if you call this function from your theme, then it should work.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange